Dictionary operations and dictionary functions in python
- 2020-06-23 00:49:37
- OfStack
The dictionary
dict_fruit = {'apple':' apple ','banana':' banana ','cherry':' cherry ','avocado':' avocado ','watermelon':' watermelon '}
Dictionary operation
# The way a dictionary is traversed
# The default traversal ( traverse key)
for value in dict_fruit:
print(value)
'''''
The value of the traversal :
watermelon
apple
cherry
avocado
banana
'''
# use key traverse ( With default traversal 1 sample )
for key in dict_fruit.keys():
print(key)
'''''
The value of the traversal :
watermelon
apple
cherry
avocado
banana
'''
# use value traverse
for value in dict_fruit.values():
print(value)
'''''
The value of the traversal :
apple
avocado
banana
watermelon
cherry
'''
# use key,value traverse
for key,value in dict_fruit.items():
print(key+'--->'+value)
'''''
The value of the traversal :
avocado---> avocado
apple---> apple
banana---> banana
cherry---> cherry
watermelon---> watermelon
'''
# Create a dictionary
# use dict()
res = dict(brand = ' brand ',size=' size ',color=' color ')
print(res,type(res))
'''''
res The results of :
{'size': ' size ', 'brand': ' brand ', 'color': ' color '} <class 'dict'>
'''
# use zip() and dict()
keys = ['1','2','3','4','5']
values = [1,2,3,4,5]
res = dict(zip(keys,values))
print(res,type(res))
'''''
res The results of :
{'3': 3, '4': 4, '1': 1, '2': 2, '5': 5} <class 'dict'>
'''
# The derivation of the dictionary
res = {k+' The Chinese is '+v for k,v in dict_fruit.items()}
print(res)
'''''
res The results of :
{'watermelon Chinese is watermelon ', 'avocado It's avocado in Chinese ', 'banana The Chinese is banana ', 'cherry Chinese is cherry ', 'apple The Chinese is apple '}
'''
Dictionary function
# Empty dictionary
test1 = {1:'1'}
test1.clear()
print(test1)
'''''
test1 The results of :
{}
'''
# Copy a dictionary ( Copied into 1 A new dictionary )
test2 = {2:'2'}
test2_copy = test2.copy()
print(test2_copy)
'''''
test2 The results of :
{2: '2'}
'''
# Use the specified key and value making 1 A dictionary
list_test = ['a','b','c']
test3 = {}.fromkeys(list_test,'ojbk')
print(test3)
'''''
test3 The results of :
{'a': 'ojbk', 'b': 'ojbk', 'c': 'ojbk'}
'''
# will 1 The number of dictionaries is converted to 2 Level of container ( The middle container )
res = dict_fruit.items()
print(res,type(res))
'''''
res The results of :
dict_items([('avocado', ' avocado '), ('apple', ' apple '), ('banana', ' banana '), ('watermelon', ' watermelon '), ('cherry', ' cherry ')]) <class 'dict_items'>
'''
# The dictionary key Make up a new container
res = dict_fruit.keys()
print(res,type(res))
'''''
res The results of :
dict_keys(['watermelon', 'cherry', 'avocado', 'apple', 'banana']) <class 'dict_keys'>
'''
# The dictionary value Make up a new container
res = dict_fruit.values()
print(res,type(res))
'''''
res The results of :
dict_values([' avocado ', ' banana ', ' cherry ', ' apple ', ' watermelon ']) <class 'dict_values'>
'''
# According to the key Delete the data in the dictionary
test4 = {1:'1',2:'2',3:'3'}
test4.pop(2)
print(test4)
'''''
test4 The results of :
{1: '1', 3: '3'}
'''
# In turn, the pop-up ( delete ) Dictionary data
test5 = {1:'1',2:'2',3:'3',4:'4',5:'5'}
test5.popitem()
print(test5)
test5.popitem()
print(test5)
test5.popitem()
print(test5)
'''''
test5 The results in turn :
{2: '2', 3: '3', 4: '4', 5: '5'}
{3: '3', 4: '4', 5: '5'}
{4: '4', 5: '5'}
'''
# update dict The data in the ( update 1 One that doesn't exist key when , Can be used to add new data )
test6 = {'super':'Eric','ssuper':'Cbabe','sssuper':'Gogo','supreme':'wiz333'}
# Update the data
test6.update(super='Eric-LPL')
print(test6)
# Add data
test6.update(niceboy='Bigmao')
print(test6)
'''''
test6 The results in turn :
{'ssuper': 'Cbabe', 'supreme': 'wiz333', 'sssuper': 'Gogo', 'super': 'Eric-LPL'}
{'ssuper': 'Cbabe', 'supreme': 'wiz333', 'niceboy': 'Bigmao', 'sssuper': 'Gogo', 'super': 'Eric-LPL'}
'''
# To obtain dict The data in the ( use key To obtain )
test7 = {1:'1',2:'2',3:'3',4:'4',5:'5'}
res = test7.get(1)
print(res,type(res))
'''''
test7 The results of :
1 <class 'str'>
'''
# to dict Add data (setdefault, Cannot be used to update data )
test8 = {1:'1',2:'2',3:'3',4:'4',5:'5'}
test8.setdefault(6,'6')
print(test8)
'''''
test8 The results of :
{1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6'}
'''
conclusion