创建字典方式

# 创建字典的几种方式:

# 方式1:
dic = dict((('one', 1),('two', 2),('three', 3)))
# dic = dict([('one', 1),('two', 2),('three', 3)])
print(dic)  # {'one': 1, 'two': 2, 'three': 3}


# 方式2:
dic = dict(one=1,two=2,three=3)
print(dic)  # {'one': 1, 'two': 2, 'three': 3}


# 方式3:
dic = dict({'one': 1, 'two': 2, 'three': 3})
print(dic)  # {'one': 1, 'two': 2, 'three': 3}

# 方式5: 后面会讲到先了解
dic = dict(zip(['one', 'two', 'three'],[1, 2, 3]))
print(dic)

# 方式6: 字典推导式 后面会讲到
# dic = { k: v for k,v in [('one', 1),('two', 2),('three', 3)]}
# print(dic)

# 方式7:利用fromkey后面会讲到。
# dic = dict.fromkeys('abcd','太白')
# print(dic)  # {'a': '太白', 'b': '太白', 'c': '太白', 'd': '太白'}
原文地址:https://www.cnblogs.com/jiazeng/p/10644030.html