字典生成式,啥是字典生成式?字典生成式需要注意什么?


# 字典生成式
print('字典的值是:',{i: i**3 for i in range(10)})

# --------反推字典生成式------------
dic={}
for n in range(10):
    dic[n]=n**3

print('我的值是',dic)

字典的值是: {0: 0, 1: 1, 2: 8, 3: 27, 4: 64, 5: 125, 6: 216, 7: 343, 8: 512, 9: 729}
我的值是 {0: 0, 1: 1, 2: 8, 3: 27, 4: 64, 5: 125, 6: 216, 7: 343, 8: 512, 9: 729}

原文地址:https://www.cnblogs.com/ludundun/p/14100324.html