1
enumerate()函数
当遍历一个非数值序列时,有时候会需要将元素和索引一起取出,这时候便可以用到enumerate()函数。...(3, 'Fall'), (4, 'Winter')]
这时候用for循环遍历enumerate()函数包装后的序列,就能得到元素及其索引值:
>>> for i, v in enumerate(['tic...', 'tac', 'toe']):
......print(i, v)
...
0 tic
1 tac
2 toe
2
字典item()方法
遍历字典时,如果直接遍历字典对象,只能得到字典中的键:
>>> sample = {'a':1,'b':2,...>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> list(zipped)
[(1, 4), (2, 5), (3, 6)]