python字典创建,删除,增加,合并等方法的调用

1:创建字典
2:为字典增加一项
3:访问字典中的值
4:删除字典中的一项
5:字典的遍历
6:keys():tuple 返回一个包含字典所有key的列表
7:values():tuple 返回一个包含字典所有value的列表
8:items():tuple 返回一个包含所有键值的列表
9:clear():None 删除字典中的所有项目
10:get(key):value 返回字典中key对应的值
11:pop(key):value 删除并返回字典中key对应的值
12:updata(NewDictionaryName) 将字典中的键值添加到字典中

'''创建一个字典'''
dict1={
    
    "luichun":"大佬","intel":"amd"}
print(dict1)
'''为字典增加一项    dictionaryName[key]= Value'''
dict1["luidage"] = "大佬"
print(dict1)
'''访问字典中的值'''
dict1["luidage"]
print(dict1["luidage"])
'''删除字典中的一项 del dictionaryName[key]'''
del dict1["intel"]
print(dict1)
'''字典的遍历   for key in dictionaryName:'''
for key in dict1:
    print(key + ":" + str(dict1[key]))
# 简写模式
'''遍历字典的键key'''
for key in dict1.keys():print(key)
'''遍历字典的值value'''
for value in dict1.values():print(value)
'''遍历字典的项'''
for item in dict1.items():print(item)
'''遍历字典的key-value'''
for item,value in dict1.items():print(item,value)
'''查询一个键在字典中'''
print("intel" in dict1)
'''不在字典中'''
print("intel" not in dict1)
'''判断两个字典是否相等'''
# 创建一个新字典
dict2 = {
    
    "luichun":"大佬","luidage":"大佬"}
print(dict1 == dict2)
print(dict1 != dict2)
'''
keys():tuple 返回一个包含字典所有key的列表
values():tuple 返回一个包含字典所有value的列表
items():tuple 返回一个包含所有键值的列表
clear():None 删除字典中的所有项目
get(key):value 返回字典中key对应的值
pop(key):value 删除并返回字典中key对应的值
updata(NewDictionaryName) 将字典中的键值添加到字典中
'''
a=tuple(dict1.keys())
print(a)
b=tuple(dict1.values())
print(b)
c=tuple(dict1.items())
print(c)
d=dict1.get("luichun")
print(d)
e=dict1.pop("luichun")
print(e)
f=dict1.clear()
print(f)
g= {
    
    "jinrou":"金柔"}
dict2.update(g)
print(dict2)

猜你喜欢

转载自blog.csdn.net/weixin_47021806/article/details/113934975