程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Operations related to Python dictionary

編輯:Python

Adding and modifying dictionaries

# coding:utf-8
if __name__ == '__main__':
example = {
'name': 'xie', 'age': 27}
example2 = example.copy()
example3 = example.copy()
''' example 1: Use dict[key] = value, This method if dict There is the key, Then modify the key Value on =value, There is no such thing as key, Then create the key=>value example 2: Use dict.setdefault(key,value), When dict There is the key, Then do nothing . When dict There is no such thing as key, Then create the key=>value dict.setdefault(key,value) Returns the dict[key] Value example 3: Use dict.update(dict2), It can be understood as a combination of two dictionaries , Repeat with dict2 Of key=>value Subject to '''
# example 1
example['school'] = ' Chengnan '
print(example) # {'name': 'xie', 'age': 27, 'school': ' Chengnan '}
example['name'] = 'rui'
print(example) # {'name': 'rui', 'age': 27, 'school': ' Chengnan '}
# example 2
result = example2.setdefault('name', 'rui')
print(result) # xie
print(example2) # {'name': 'xie', 'age': 27}
result = example2.setdefault('school', ' Chengnan ')
print(result) # Chengnan 
print(example2) # {'name': 'xie', 'age': 27, 'school': ' Chengnan '}
# example 3
example3.update({
'name': 'rui'})
print(example3) # {'name': 'rui', 'age': 27}
example3.update({
'name': 'xiang', 'school': ' Chengnan '})
print(example3) # {'name': 'xiang', 'age': 27, 'school': ' Chengnan '}

Get all the dictionary's key

# coding:utf-8
if __name__ == '__main__':
''' example 1: adopt dict.keys() obtain dict all key A pseudo list Pseudo list : You can only see . Do not modify , Remove elements adopt list(dict_keys) Can change a pseudo list into a real list '''
# example 1
example = {
'name': 'xie', 'age': 27, 'sex': ' male '}
keys = example.keys()
print(keys) # dict_keys(['name', 'age', 'sex'])
print(type(keys)) # <class 'dict_keys'>
print(type(list(keys))) # <class 'list'>

Get all the dictionary's value

# coding:utf-8
if __name__ == '__main__':
''' example 1: adopt dict.values() obtain dict All in value A pseudo list adopt list(dict_keys) Convert a pseudo list to a real list '''
# example 1
example = {
'name': 'xie', 'age': 27, 'sex': 'man'}
values = example.values()
print(values) # dict_keys(['name', 'age', 'sex'])
print(type(values)) # <class 'dict_keys'>
print(type(list(values))) # <class 'list'>

Get the dictionary key Upper valule

# coding:utf-8
if __name__ == '__main__':
''' example 1: adopt dict[key] obtain value, If key If it doesn't exist, it's wrong Column 2: adopt dict.get(key,default) obtain key, If key Returns if it does not exist default,default Default None, Performance ratio dict[key] Xiaoyidididiu , But don't worry about reporting errors '''
example = {
'name': 'xie', 'age': 27, 'sex': 'man'}
# example 1
print(example['name']) # xie
# print(example['school']) Report errors 
# example 2
print(example.get('name')) # xie
print(example.get('school')) # None
print(example.get('school', ' The flowers in the south of the city have blossomed ')) # The flowers in the south of the city have blossomed 

The dictionary is empty

# coding:utf-8
if __name__ == '__main__':
''' example 1:dict.clear() Get an empty dictionary '''
example = {
'name': 'xie'}
# example 1
example.clear()
print(example) # {}

Delete dictionary elements by index

# coding:utf-8
if __name__ == '__main__':
''' example 1:dict.pop(key) Delete Dictionary key Upper key=>value, If key non-existent , False report dict.pop(key) Return to before deletion dict[key] Upper value '''
example = {
'name': 'xie', 'age': 27}
print(example.pop('age')) # 27
print(example) # {'name': 'xie'}
# example.pop('sex') sex Does not exist in dictionary , Report errors 

Delete the last element of the dictionary

# coding:utf-8
if __name__ == '__main__':
''' example 1:dict.popitem() Delete the... Of the last element key=>value, If dict If the dictionary is empty, an error is reported dict.popitem() Return the deleted element key,value A tuple of components (key,value) '''
example = {
'name': 'xie', 'age': 27}
# example 1
print(example.popitem()) # ('age', 27)
print(example) # {'name': 'xie'}
example.popitem()
# example.popitem() Empty dictionary calls popitem Will report a mistake 

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved