dict={'name':'Joe','age':18,'height':60}
clear, clear
()
#Running results {}
pop, removes the key-value pair with the specified key and returns the vlaue (or the specified value if there is no such key), popitem, removes the last key-value pair by default
print(('age'))
print(dict)
#Result 18, {'name': 'Joe', 'height': 60}
print(('agea','erro'))
print(dict)
#results erro, {'name': 'Joe', 'age': 18, 'height': 60}
print(())
print(dict)
#result('height', 60), {'name': 'Joe', 'age': 18}
del, another way to delete a dictionary
del dict['age']
print(dict)
#results {'name': 'Joe', 'height': 60}
get, return the value of the specified key, if the value is not in the dictionary return default value, equivalent to dict.__getitem__('name')
print(('name'))
#Results Joe
print(('hobby'))
#ResultsNone
print(('hobby','basketball'))
#resultsbasketball
setdefault, similar to get(), but if the key does not exist in the dictionary, the key will be added and the value will be set to default.
print(('hobby'))
print(dict)
#in the endNone,{'name': 'Joe', 'age': 18, 'height': 60, 'hobby': None}
print(('hobby','basketball'))
print(dict)
#in the endbasketball,{'name': 'Joe', 'age': 18, 'height': 60, 'hobby': 'basketball'}
update, update the dictionary, if there is a key, then update the vlaue corresponding to the key, if not, then add new
({'age':20})
print(dict)
#results {'name': 'Joe', 'age': 20, 'height': 60}
({'hobby':'run'})
print(dict)
#results {'name': 'Joe', 'age': 18, 'height': 60, 'hobby': 'run'}
fromkeys, create a new dictionary with seq as the key and vlaue as the initial value of the dictionary
seq = ('a', 'b', 'c')
print((seq))
#Results {'a': None, 'b': None, 'c': None}
print((seq,'oh'))
#Results {'a': 'oh', 'b': 'oh', 'c': 'oh'}
Dictionary printing, value taking, etc.
print(())
print(())
print(())
#Results
dict_items([('name', 'Joe'), ('age', 18), ('height', 60)])
dict_values(['Joe', 18, 60])
dict_keys(['name', 'age', 'height'])
Dictionary traversal, traversing key
for i in dict:
print(i)
#Results
name
age
height
# Traversal of the same effect is as follows:
for key in ():
print(key)
#
Dictionary traversal, traversal of value
for vlaue in ():
print(vlaue)
#Results
Joe
18
60
Dictionary traversal, traversing item
#10.1 The way the output is a tuple
for item in ():
print(item)
#Results
('name', 'Joe')
('age', 18)
('height', 60)
#10.2 Output as a String
for key,vlaue in ():
print(key,vlaue)
#Results
name Joe
age 18
height 60
Another way to #output as a string
for i in dict:
print(i,dict[i])
This is the whole content of this article.