python 字典常用函数

# dict.clear() 删除字典内所有元素
dicttemp = {'Name': 'Cara', 'Age': 7}
dicttemp.clear()
print(dicttemp) #{}
#dict.fromkeys(seq[, val])
# 创建一个新字典,以序列 seq 中元素做字典的键,val 为字典所有键对应的初始值,默认为 None
s = ['e','r','t']
w = dict.fromkeys(s)
print(w) #{'e': None, 'r': None, 't': None}
#默认值为10
w = dict.fromkeys(s,10)
print(w) #{'e': 10, 'r': 10, 't': 10}
#默认值为列表
d = [1,2]
# d = ()
# d = {}
w = dict.fromkeys(s,d)
print(w) #{'e': [1, 2], 'r': [1, 2], 't': [1, 2]}
# dict.get(key, default=None)
# 返回指定键的值,如果值不在字典中返回default值
#dict[key] 在 key(键)不在字典中时,会触发 KeyError 异常。
dicttemp = {'Name': 'Cara', 'Age': 7}
ret = dicttemp.get('Name')
print(ret) #Cara
#如果健不存在,返回默认值None
ret = dicttemp.get('Class')
print(ret) #None
#如果健不存在,返回设定的默认值
ret = dicttemp.get('Class',10)
print(ret) #10
#get() 方法对嵌套字典的使用
dicttemp = {'Name': 'Cara', 'Age': 7, 'Adr':{'City':'He'}}
ret = dicttemp.get('Adr').get('City')
print(ret) #He
# dict.items()
# 以列表返回可遍历的(键, 值) 元组数组
print(dicttemp.items()) #dict_items([('Name', 'Cara'), ('Age', 7), ('Adr', {'City': 'He'})])
for key, vlue in dicttemp.items():
    print(key, vlue)
#输出结果:
'''
Name Cara
Age 7
Adr {'City': 'He'}
'''
# dict.keys()
# 以列表返回一个字典所有的键
print(dicttemp.keys())#dict_keys(['Name', 'Age', 'Adr'])
# dict.values()
# 以列表返回字典中的所有值
print(dicttemp.values()) #dict_values(['Cara', 7, {'City': 'He'}])
# dict.setdefault(key, default=None)
# 和get()类似, 但如果键不存在于字典中,将会添加键并将值设为default
# setdefault 如果不存在会在原字典里添加一个 key:default_value 并返回 default_value。
# get 找不到 key 的时候不会修改原字典,只返回 default_value。
#默认值None
dicttemp.setdefault('sex')
print(dicttemp) #{'Name': 'Cara', 'Age': 7, 'Adr': {'City': 'He'}, 'sex': None}
#设置默认值
dicttemp.setdefault('sex1','boy')
print(dicttemp)#{'Name': 'Cara', 'Age': 7, 'Adr': {'City': 'He'}, 'sex': None, 'sex1': 'boy'}
dicttemp.setdefault('phone',0)
print(dicttemp) #{'Name': 'Cara', 'Age': 7, 'Adr': {'City': 'He'}, 'sex': None, 'sex1': 'boy', 'phone': 0}
# dict.update(dict2)
# 把字典dict2的键/值对更新到dict里
d2 = {'a':1, 'b':2, 'c':3}
d1 = {'g': 10,'h':100}
d2.update(d1)
print(d2) #{'a': 1, 'b': 2, 'c': 3, 'g': 10, 'h': 100}
#有相同的键时,会使用最新的字典值
d3 = {'g': 111,'h':100}
d1.update(d3)
print(d1) #{'g': 111, 'h': 100}
# pop(key[,default])
# 删除字典给定键 key 所对应的值,返回值为被删除的值。key值必须给出。 否则,返回default值。
item = dicttemp.pop('Adr')
print(item) #{'City': 'He'}
item = dicttemp.pop('Adr','opo')
print(item) #opo
item = dicttemp.pop('Ad','健不存在')
print(item) #健不存在
# popitem()
# 返回并删除字典中的最后一对键和值。
rets = dicttemp.popitem()
print(rets) #('phone', 0)
#dict.copy()
# 返回一个字典的浅复制,
d3 = {'g': 111,'h':[1,2,3]}
dict2 = d3          # 浅拷贝: 引用对象
dict3 = d3.copy()   # 浅拷贝:深拷贝父对象(一级目录),子对象(二级目录)不拷贝,还是引用

d3['g'] = 000
d3['h'].append(10000)
print(d3) #{'g': 0, 'h': [1, 2, 3, 10000]}
print(dict2)#{'g': 0, 'h': [1, 2, 3, 10000]}
print(dict3) #{'g': 111, 'h': [1, 2, 3, 10000]}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值