标签:循环 删掉 info stat 添加 war key值 不可 参数设置
for item in info.keys():
print(item)
for item in info.values():
print(item)
for k, v in info.items():
print(k, v)
>>> new_dict = dict.fromkeys([‘k1‘,123,‘999‘],123)
>>> print(new_dict)
{‘k1‘: 123, 123: 123, ‘999‘: 123}
>>> info = {‘K1‘:123,‘k2‘:890}
>>> v = info.get(‘k1111‘,222)
>>> print (v)
222
>>> info = {‘K1‘:123,‘k2‘:890}
>>> v = info.pop(‘K1‘)
>>> print (info, v)
({‘k2‘: 890}, 123)
>>> info = {‘K1‘:123,‘k2‘:890}
>>> k, v = info.popitem()
>>> print(info, k, v)
({‘K1‘: 123}, ‘k2‘, 890) #随机删掉了K2键值对
>>> info = {‘K1‘:123,‘k2‘:890}
>>> v = info.setdefault(‘k11111‘,666) #这是当参数的键在字典中不存在时,结果把参数设置的键值对添加到了字典中,并且返回了该键对应的值。
>>> print(info, v)
({‘k2‘: 890, ‘K1‘: 123, ‘k11111‘: 666}, 666)
>>> info = {‘k1‘:123,‘k2‘:890}
>>> info.update({‘k1‘: 11111,‘k3‘:‘hdjkdl‘}) #这里参数传入的是一个字典
>>> print(info)
{‘k3‘: ‘hdjkdl‘, ‘k2‘: 890, ‘k1‘: 11111}
>>> info = {‘k1‘:123,‘k2‘:890}
>>> info.update(k1=11111,k3=‘hdjkdl‘) #或者也可以以这种方式传入参数和值
>>> print(info)
{‘k3‘: ‘hdjkdl‘, ‘k2‘: 890, ‘k1‘: 11111}
>>> dic = {‘k1‘:‘v1‘}
>>> ‘k1‘ in dic #键是True
True
>>> dic = {‘k1‘:‘v1‘}
>>> ‘v1‘ in dic #值是False
False
标签:循环 删掉 info stat 添加 war key值 不可 参数设置
原文地址:https://www.cnblogs.com/eleanorye/p/10132950.html