Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1778477
  • 博文数量: 276
  • 博客积分: 1574
  • 博客等级: 上尉
  • 技术积分: 2894
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-26 23:23
个人简介

生活的美妙在于,不知道一下秒是惊艳还是伤神,时光流转,珍惜现在的拥有的时光

文章分类

全部博文(276)

文章存档

2017年(17)

2016年(131)

2015年(63)

2013年(2)

2012年(32)

2011年(31)

分类: Python/Ruby

2016-02-18 11:55:11


点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. #-*- coding:utf8 -*-
  3. from __future__ import print_function
  4. #迭代器与解析,第一部分
  5. #列表解析是对迭代中的项应用一个表达式的for循环的一种近似形式。


  6. '''
  7. 迭代器初探
  8. for 循环可以用于python中任何的序列类型,如列表、元组、及字符串。实际上for循环可以用于任何可迭代类型。
  9. 迭代器包括:for循环、列表解析、in成员关系测试、map内置函数等。
  10. '''
  11. f=open('1.txt')
  12. print(f.readline())
  13. print(f.readline())
  14. print(f.readline())
  15. print(f.readline())
  16. print(f.readline())
  17. #文件有一个方法__next__实现差不多的功能,只不过结束时返回stopiteration
  18. #f=open('1.txt')
  19. #print(f.__next__())
  20. #逐行读取文件的最好方法就是不要去读,让for循环在每轮自动调用__next__从而进入到下一行。
  21. #这是读取文件的最佳方法,原因:运行最快,内存最省,书写简单。迭代器在python是以C语言的速度运行的。
  22. for line in open('1.txt'):
  23.     print(line.upper(),end='')

  24. '''
  25. 手工迭代:iter,next
  26.     基于可迭代对象X,next(X).__next__()

  27. '''
  28. f=open('1.txt')
  29. print(next(f))
  30. print(next(f))
  31. print(next(f))
  32. print(next(f))

  33. f=open('1.txt')
  34. print(iter(f) is f)
  35. print(next(iter(f)))
  36. print(next(f))

  37. L=[1,2,3]
  38. for x in L:
  39.     print(x,end=(' '))
  40. I=iter(L)
  41. while True:
  42.     try:
  43.         X=next(I)
  44.     except StopIteration:
  45.         break
  46.     print(X,end='_')

  47. '''
  48. 其它内置类型迭代器
  49. '''
  50. #遍历字典键的经典方法是明确的获取其键的列表。
  51. D={
  52.     'a':1,'b':2,'c':3
  53. }
  54. for key in D.keys():
  55.     print(key,':',D[key])
  56. #最近的python添加了字典的迭代器,可以直接使用for循环,不再需要使用keys方法。
  57. for key in D:
  58.     print(key,'=>',D[key])
  59. I=iter(D)
  60. print(I.next())
  61. print(I.next())
  62. print(I.next())
  63. #迭代协议也是我们必须把某些结果包装到一个list调用中一次性看到它们的值的原因
  64. #python中可以从左到右扫描的所有对象都以同样的方式实现迭代。
  65. print(list(D))
  66. #enumerate
  67. E=enumerate('talen')
  68. I=iter(E)
  69. print(list(I))

  70. '''
  71. 列表解析:初探 更简洁,更快。
  72. '''
  73. #列表解析使得早先许多例子变得过时了。列表解析在python中以C语言的速度执行。
  74. L=[1,2,3,4]
  75. L=[l + 10 for l in L]
  76. print(L)
  77. #在文件上使用列表解析
  78. f=open('1.txt')
  79. lines=f.readlines()
  80. print(lines)
  81. #去除后面的换行符rstrip()
  82. lines=[line.rstrip() for line in lines]
  83. print(lines)
  84. #最终优化后的语句,列表解析自动利用迭代协议构建了操作结果的一个列表.这是高效率的操作:大多数操作在python解释器内部完成,这经等价语句快得多。
  85. lines=[line.rstrip() for line in open('1.txt')]
  86. print(lines)
  87. lines=[line.upper() for line in open('1.txt')]
  88. print(lines)
  89. lines=[line.rstrip().upper() for line in open('1.txt')]
  90. print(lines)
  91. lines=[line.split() for line in open('1.txt')]
  92. print(lines)
  93. lines=[line.replace(' ','!') for line in open('1.txt')]
  94. print(lines)
  95. lines=[('talen' in line , line[0]) for line in open('1.txt')]
  96. print(lines)

  97. #扩展列表解析语法
  98. #列表解析语法允许任意数的for循环子句,每一个子句都有一个可选的if子句。
  99. #取出以'#'号开头的行
  100. lines=[line.rstrip() for line in open('1.txt') if line[0] == '#']
  101. print(lines)
  102. print([x+y for x in 'abc' for y in '123'])

  103. #其它迭代环境
  104. #用户定义的类也可以实现,for循环、列表解析、in成员关系测试、map内置函数以及像sorted,enumerate,reduce,zip调用,sum,any,all,max,min这样的内置函数也都使用了迭代协议。
  105. print('import' in open('1.txt'))
  106. print('python' in open('1.txt'))
  107. #python3.0中新的可迭代对象
  108. #range迭代器
  109. #map,,filter可迭代对象
  110. #字典迭代器


点击(此处)折叠或打开

  1. /usr/bin/python2.7 /home/talen/PycharmProjects/untitled/14.py
  2. #!/usr/bin/env python

  3. #-*- coding:utf8 -*-

  4. from __future__ import print_function




  5. #!/USR/BIN/ENV PYTHON
  6. #-*- CODING:UTF8 -*-
  7. FROM __FUTURE__ IMPORT PRINT_FUNCTION

  8. #!/usr/bin/env python

  9. #-*- coding:utf8 -*-

  10. from __future__ import print_function



  11. True
  12. #!/usr/bin/env python

  13. #-*- coding:utf8 -*-

  14. 1 2 3 1_2_3_a : 1
  15. c : 3
  16. b : 2
  17. a => 1
  18. c => 3
  19. b => 2
  20. a
  21. c
  22. b
  23. ['a', 'c', 'b']
  24. [(0, 't'), (1, 'a'), (2, 'l'), (3, 'e'), (4, 'n')]
  25. [11, 12, 13, 14]
  26. ['#!/usr/bin/env python\n', '#-*- coding:utf8 -*-\n', 'from __future__ import print_function\n', '\n']
  27. ['#!/usr/bin/env python', '#-*- coding:utf8 -*-', 'from __future__ import print_function', '']
  28. ['#!/usr/bin/env python', '#-*- coding:utf8 -*-', 'from __future__ import print_function', '']
  29. ['#!/USR/BIN/ENV PYTHON\n', '#-*- CODING:UTF8 -*-\n', 'FROM __FUTURE__ IMPORT PRINT_FUNCTION\n', '\n']
  30. ['#!/USR/BIN/ENV PYTHON', '#-*- CODING:UTF8 -*-', 'FROM __FUTURE__ IMPORT PRINT_FUNCTION', '']
  31. [['#!/usr/bin/env', 'python'], ['#-*-', 'coding:utf8', '-*-'], ['from', '__future__', 'import', 'print_function'], []]
  32. ['#!/usr/bin/env!python\n', '#-*-!coding:utf8!-*-\n', 'from!__future__!import!print_function\n', '\n']
  33. [(False, '#'), (False, '#'), (False, 'f'), (False, '\n')]
  34. ['#!/usr/bin/env python', '#-*- coding:utf8 -*-']
  35. ['a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'c1', 'c2', 'c3']
  36. False
  37. False

  38. Process finished with exit code 0

1.txt

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. #-*- coding:utf8 -*-
  3. from __future__ import print_function

阅读(1184) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~