点击(此处)折叠或打开
- #!/usr/bin/env python
- #-*- coding:utf8 -*-
- from __future__ import print_function
- #迭代器与解析,第一部分
- #列表解析是对迭代中的项应用一个表达式的for循环的一种近似形式。
-
-
- '''
- 迭代器初探
- for 循环可以用于python中任何的序列类型,如列表、元组、及字符串。实际上for循环可以用于任何可迭代类型。
- 迭代器包括:for循环、列表解析、in成员关系测试、map内置函数等。
- '''
- f=open('1.txt')
- print(f.readline())
- print(f.readline())
- print(f.readline())
- print(f.readline())
- print(f.readline())
- #文件有一个方法__next__实现差不多的功能,只不过结束时返回stopiteration
- #f=open('1.txt')
- #print(f.__next__())
- #逐行读取文件的最好方法就是不要去读,让for循环在每轮自动调用__next__从而进入到下一行。
- #这是读取文件的最佳方法,原因:运行最快,内存最省,书写简单。迭代器在python是以C语言的速度运行的。
- for line in open('1.txt'):
- print(line.upper(),end='')
-
- '''
- 手工迭代:iter,next
- 基于可迭代对象X,next(X).__next__()
-
- '''
- f=open('1.txt')
- print(next(f))
- print(next(f))
- print(next(f))
- print(next(f))
-
- f=open('1.txt')
- print(iter(f) is f)
- print(next(iter(f)))
- print(next(f))
-
- L=[1,2,3]
- for x in L:
- print(x,end=(' '))
- I=iter(L)
- while True:
- try:
- X=next(I)
- except StopIteration:
- break
- print(X,end='_')
-
- '''
- 其它内置类型迭代器
- '''
- #遍历字典键的经典方法是明确的获取其键的列表。
- D={
- 'a':1,'b':2,'c':3
- }
- for key in D.keys():
- print(key,':',D[key])
- #最近的python添加了字典的迭代器,可以直接使用for循环,不再需要使用keys方法。
- for key in D:
- print(key,'=>',D[key])
- I=iter(D)
- print(I.next())
- print(I.next())
- print(I.next())
- #迭代协议也是我们必须把某些结果包装到一个list调用中一次性看到它们的值的原因
- #python中可以从左到右扫描的所有对象都以同样的方式实现迭代。
- print(list(D))
- #enumerate
- E=enumerate('talen')
- I=iter(E)
- print(list(I))
-
- '''
- 列表解析:初探 更简洁,更快。
- '''
- #列表解析使得早先许多例子变得过时了。列表解析在python中以C语言的速度执行。
- L=[1,2,3,4]
- L=[l + 10 for l in L]
- print(L)
- #在文件上使用列表解析
- f=open('1.txt')
- lines=f.readlines()
- print(lines)
- #去除后面的换行符rstrip()
- lines=[line.rstrip() for line in lines]
- print(lines)
- #最终优化后的语句,列表解析自动利用迭代协议构建了操作结果的一个列表.这是高效率的操作:大多数操作在python解释器内部完成,这经等价语句快得多。
- lines=[line.rstrip() for line in open('1.txt')]
- print(lines)
- lines=[line.upper() for line in open('1.txt')]
- print(lines)
- lines=[line.rstrip().upper() for line in open('1.txt')]
- print(lines)
- lines=[line.split() for line in open('1.txt')]
- print(lines)
- lines=[line.replace(' ','!') for line in open('1.txt')]
- print(lines)
- lines=[('talen' in line , line[0]) for line in open('1.txt')]
- print(lines)
-
- #扩展列表解析语法
- #列表解析语法允许任意数的for循环子句,每一个子句都有一个可选的if子句。
- #取出以'#'号开头的行
- lines=[line.rstrip() for line in open('1.txt') if line[0] == '#']
- print(lines)
- print([x+y for x in 'abc' for y in '123'])
-
- #其它迭代环境
- #用户定义的类也可以实现,for循环、列表解析、in成员关系测试、map内置函数以及像sorted,enumerate,reduce,zip调用,sum,any,all,max,min这样的内置函数也都使用了迭代协议。
- print('import' in open('1.txt'))
- print('python' in open('1.txt'))
- #python3.0中新的可迭代对象
- #range迭代器
- #map,,filter可迭代对象
- #字典迭代器
点击(此处)折叠或打开
- /usr/bin/python2.7 /home/talen/PycharmProjects/untitled/14.py
- #!/usr/bin/env python
-
- #-*- coding:utf8 -*-
-
- from __future__ import print_function
-
-
-
-
- #!/USR/BIN/ENV PYTHON
- #-*- CODING:UTF8 -*-
- FROM __FUTURE__ IMPORT PRINT_FUNCTION
-
- #!/usr/bin/env python
-
- #-*- coding:utf8 -*-
-
- from __future__ import print_function
-
-
-
- True
- #!/usr/bin/env python
-
- #-*- coding:utf8 -*-
-
- 1 2 3 1_2_3_a : 1
- c : 3
- b : 2
- a => 1
- c => 3
- b => 2
- a
- c
- b
- ['a', 'c', 'b']
- [(0, 't'), (1, 'a'), (2, 'l'), (3, 'e'), (4, 'n')]
- [11, 12, 13, 14]
- ['#!/usr/bin/env python\n', '#-*- coding:utf8 -*-\n', 'from __future__ import print_function\n', '\n']
- ['#!/usr/bin/env python', '#-*- coding:utf8 -*-', 'from __future__ import print_function', '']
- ['#!/usr/bin/env python', '#-*- coding:utf8 -*-', 'from __future__ import print_function', '']
- ['#!/USR/BIN/ENV PYTHON\n', '#-*- CODING:UTF8 -*-\n', 'FROM __FUTURE__ IMPORT PRINT_FUNCTION\n', '\n']
- ['#!/USR/BIN/ENV PYTHON', '#-*- CODING:UTF8 -*-', 'FROM __FUTURE__ IMPORT PRINT_FUNCTION', '']
- [['#!/usr/bin/env', 'python'], ['#-*-', 'coding:utf8', '-*-'], ['from', '__future__', 'import', 'print_function'], []]
- ['#!/usr/bin/env!python\n', '#-*-!coding:utf8!-*-\n', 'from!__future__!import!print_function\n', '\n']
- [(False, '#'), (False, '#'), (False, 'f'), (False, '\n')]
- ['#!/usr/bin/env python', '#-*- coding:utf8 -*-']
- ['a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'c1', 'c2', 'c3']
- False
- False
-
- Process finished with exit code 0
1.txt
点击(此处)折叠或打开
- #!/usr/bin/env python
- #-*- coding:utf8 -*-
- from __future__ import print_function