字典键的特性:
1)键值的唯一性,如果同一个键被赋值两次,后一个值被保存
2)键必须不可变,所以可以用数字,字符串或者元组充当键,不能用列表作为键。
eg:
dict = {'name':"jiaozi", 'age':23, 'work':"jiaozi"}
print(dict)
del dict['age']
print(dict)
dict.clear()
print(dict)
del dict
dict2 = {('name',):"Google", 'Age':7}
print(dict2)
# dict3 = {['name']: "Google", 'Age': 7}
dict4 = dict2
dict5 = dict2.copy()
print(dict4is dict2)
print(dict5is dict2)
dict2['Age'] =8
print(dict2)
print(dict4)
print(dict5)
seq = ('name', 'age')
dict6 = dict.fromkeys(seq)
print("新的字典为:{0}".format(str(dict6)))
dict6 = dict.fromkeys(seq, 10)
print("新的字典为:{0}".format(str(dict6)))
print("age值为:{0}".format(dict6.get('age')))
print("job值为:{0}".format(dict6.get('job', 'warrenwell')))
print(dict6)
print("sex值为:{0}".format(dict6.setdefault('sex', 'male')))
print("新字典为:", dict6)
网友评论