#####列表推导式
#####集合字典也可
#####元组也可
1 | a=[1,2,3,4,5,6,7,8,9] |
也可以map filter表示1
2
3
4list_a=[1,2,3,4,5,6,7,8,9]
r=filter(lambda x:x if x<=5 else 0,list_a)
s=map(lambda x:x*x,r)
print(list(s))
1 | #同上,不建议 |
#####当为字典时1
2
3
4
5
6
7
8
9students ={
'wei':18,
'lai':19,
'wan':20
}
b = [key for key,value in students.items()]
print(b)#['wei', 'lai', 'wan']
for x in b:
print(x)#wei#lai#wan
**交换key和value1
2
3
4
5
6
7
8students ={
'wei':18,
'lai':19,
'wan':20
}
b ={value:key for key,value in students.items()}
print(b)#{18: 'wei', 19: 'lai', 20: 'wan'}