Python dictionary creation, deletion, addition, merging and other methods of calling

1: Create a dictionary
2: Add an item to the dictionary
3: Access the value in the dictionary
4: Delete an item in the
dictionary 5: Traversal of the dictionary
6: keys():tupleReturn a list containing all the keys of the dictionary
7: values():tupleReturn a list containing all the values ​​of the dictionary Listing
8: items():tupleReturn a list containing all key values
9: clear():NoneDelete all items in the dictionary
10: get(key):valueReturn the value corresponding to the key in the dictionary
11: pop(key):valueDelete and return the value corresponding to the key in the dictionary
12: updata(NewDictionaryName)Add the key value in the dictionary to the dictionary in

'''创建一个字典'''
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)

Guess you like

Origin blog.csdn.net/weixin_47021806/article/details/113934975