#集合的遍历 list2=[1,2,3,4,5] print([x for x in list2]) #结果: [1, 2, 3, 4, 5] #数组的遍历 array=(3,4,5,6); r=(m for m in array) print((m for m in array).__next__()) 结果为: 3 #字典的遍历: dic={"name":"liu","age":12}; print({x:str(x) for x in dic})
#结果为:
{'name': 'name', 'age': 'age'}
#二元数组的遍历
#二元的遍历 vec=[[1,2,3],[4,5,6],[7,8,9]]; for item in vec: #print("====",item) for st in item: print(st) #print("=====") print([st for item in vec for st in item]);
结果为:
1
2
3
4
5
6
7
8
9
[1, 2, 3, 4, 5, 6, 7, 8, 9]
#zip 函数 d2=dict(zip(["apple","banana"],[1,2])); print(d2)
结果为:
{'apple': 1, 'banana': 2}