赞
踩
dic = {}
print(type(dic))
# 输出结果:<class 'dict'>
dic = {'name': 'Jack', 'age': 18, 'height': 180}
print(dic)
# 输出结果:{'name': 'Jack', 'age': 18, 'height': 180}
dic = dict(name='Jack', age=18, height=180)
print(dic)
# 输出结果:{'name': 'Jack', 'age': 18, 'height': 180}
•输出一个类似{ i : i*i }的字典
dic = dict()
for i in range(1, 5):
dic[i] = i * i
print(dic)
# 输出结果:{1: 1, 2: 4, 3: 9, 4: 16}
lis = [('name', 'Jack'), ('age', 18), ('height', 180)]
dic = dict(lis)
print(dic)
# 输出结果:{'name': 'Jack', 'age': 18, 'height': 180}
dic = dict(zip('abc', [1, 2, 3]))
print(dic)
# 输出结果:{'a': 1, 'b': 2, 'c': 3}
dic = {i: i ** 2 for i in range(1, 5)}
print(dic)
# 输出结果:{1: 1, 2: 4, 3: 9, 4: 16}
注意:通常用来初始化字典, 设置value的默认值
dic = dict.fromkeys(range(4), 'x')
print(dic)
# 输出结果:{0: 'x', 1: 'x', 2: 'x', 3: 'x'}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。