python迭代器

使用max(),min(),sum(),

对象的数据类型要一致,否则报错!

>>> k
[1, 2, 3, 'k']
>>> max(k)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'str' and 'int'


 

以下几个迭代器,返回的是对象值
reversed()    #翻转
enumerate() #枚举:[(index1,value1),(index2,value2)···]
zip()             #打包

 

>>> a = [1,2,3,4,5]
>>> reversed(a)
<list_reverseiterator object at 0x000001E8118F0198>
>>> list(reversed(a))
[5, 4, 3, 2, 1]


>>> enumerate(a)
<enumerate object at 0x000001E8118F11F8>
>>> list(enumerate(a))
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]


>>> a
[1, 2, 3, 4, 5]
>>> b = ['a','b','c','d','e']
>>> list(zip(a,b))
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]

posted @ 2019-03-23 09:30  jinfengJeff  阅读(162)  评论(0编辑  收藏  举报