resversed(序列) 顺序翻转迭代
a=list('hello')
a
['h', 'e', 'l', 'l', 'o']
a1=reversed(a) #a1是一个迭代器
print(type(a))
print(type(a1))
<class 'list'>
<class 'list_reverseiterator'>
b=[1,2,3,4,5]
b1=reversed(b)
print(type(b))
print(type(b1))
<class 'list'>
<class 'list_reverseiterator'>
reversed((1,2,4,7,8))
<reversed at 0x2272e4a82e8>
a2=reversed(range(5))
list(a2)
[4, 3, 2, 1, 0]
zip(序列1,序列2) 并行迭代
l1=['a','b','c','d']
l2=[1,2,3,4]
list(zip(l1,l2))
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
l3=('a','b','c','d')
l4=[1,2,3,4]
list(zip(l1,l2))
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
l='hello'
s=range(len(l))
list(zip(l,s))
[('h', 0), ('e', 1), ('l', 2), ('l', 3), ('o', 4)]
l=dict(keys=l,values=s)
l
{'keys': 'hello', 'values': range(0, 5)}
s
range(0, 5)
list(zip(l,s))
[('keys', 0), ('values', 1)]
编号迭代
enumerate(序列):给序列打上编号
l=['1','b','h',8]
s=enumerate(l)
s
<enumerate at 0x2272dc60b88>
list(s)
[(0, '1'), (1, 'b'), (2, 'h'), (3, 8)]
探寻有趣之事!