Python字典推导式
要求打印字典中值小于1的key和value
d = {"a": 1, "b":2, "c":3}
d = {key: value for key,value in d.items() if value <= 1 }
print(d)
结果:
{'a': 1}
value是可以直接计算的
d = {"a": 1, "b":2, "c":3}
d = {key: value*3 for key,value in d.items() if value <= 1 }
print(d)
结果:
{'a': 3}
QQ:1061767621
Q群:215481318