字典生成式
一、字典生成式
print({i:i**2 for i in range(10)})
#输出:
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
二、zip()方法
z= zip(['a','b','c','d'],[i for i in range(4)])
dic = {k:v for k,v in z}
print(dic)
#输出:
{'a': 0, 'b': 1, 'c': 2, 'd': 3}