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

The application of Python dictionary

編輯:Python

What Xiaoting is sharing with you today is Python The application of Dictionary .

Python The application of Dictionary

Python3 The dictionary contains the following built-in methods :

Serial number

Functions and descriptions

1

radiansdict.clear() Delete all elements in the dictionary

2

radiansdict.copy() Returns a shallow copy of a dictionary

3

radiansdict.fromkeys() Create a new dictionary , In sequence seq The middle element is the key of the dictionary ,val Is the initial value of all keys in the dictionary

4

radiansdict.get(key, default=None) Returns the value of the specified key , If the value is not returned in the dictionary default value

5

key in dict If the key is in the dictionary dict Back in true, Otherwise return to false

6

radiansdict.items() Return traversable as a list ( key , value ) Tuple array

7

radiansdict.keys() Return all keys of a dictionary as a list

8

radiansdict.setdefault(key, default=None) and get() similar , But if the key does not exist in the dictionary , The key will be added and the value will be set to default

9

radiansdict.update(dict2) Put the dictionary dict2 Key / Value pair update to dict in

10

radiansdict.values() Returns all values in the dictionary as a list

11

pop(key[,default]) Delete dictionary given key key The corresponding value , The return value is the deleted value .key Value must be given . otherwise , return default value .

12

popitem() Randomly return and delete a pair of keys and values in the dictionary ( Generally delete the end pair ).

Some basic considerations about Dictionaries , Make a brief introduction , Hopefully that helped !!!

d = {'key1' : 'value1', 'key2 ': 'value2','key1' : 'value3'}
print(d)
#python3 result :{'key1': 'value3', 'key2 ': 'value2'}
# The key must be unique , But values don't have to be , That is, the same key is not allowed to appear twice . When creating, if the same key is assigned twice , The latter value will be remembered .
d = {'key1' : 'value1', 'key2 ': 'value2','key3' : 'value3'}
print(d)
#python3 result :{'key1': 'value1', 'key2 ': 'value2', 'key3': 'value3'}
d = {'key1' : 'value1', 'key2 ': 'value2','key3' : 'value1'}
print(d)
#python3 result :{'key1': 'value1', 'key2 ': 'value2', 'key3': 'value1'}
d = {'key1' : 1, 'key2 ': 'a','key3' : [1,2,3]}
print(d)
#python3 result :{'key1': 1, 'key2 ': 'a', 'key3': [1, 2, 3]}
#d = {[1,2,3] : 'value1', 'a ': 'value2','3' : 'value1'}----- This is a mistake
# The value can take any data type , But the bond has to be immutable , Such as a string , A number or tuple .

Visit the values in the dictionary

d = {'key1' : 1, 'key2 ': 'a','key3' : [1,2,3]}
print(d['key1'])
#python3 result :1
# according to key Values obtained value Value , The contents of the dictionary are changeable
d['key1']=123
print(d)
#python3 result :{'key1': 123, 'key2 ': 'a', 'key3': [1, 2, 3]}
d['key4']={1,2,3}
print(d)
#python3 result :{'key1': 123, 'key2 ': 'a', 'key3': [1, 2, 3], 'key4': {1, 2, 3}}
# If key The value is , It's equivalent to modifying the value of an element ; If it doesn't exist , It's equivalent to adding a key value pair .

Remove elements

del d['key4']
print(d)
#python3 result :{'key1': 123, 'key2 ': 'a', 'key3': [1, 2, 3]}

Empty key value pairs

d.clear()
print(d)
#python3 result :{}

Count the number of dictionary elements , That's the total number of bonds .

d = {'key1' : 'value1', 'key2 ': 'value2','key3' : 'value3'}
print(len(d))
#python3 result :3

Convert to string

d = {'key1' : 'value1', 'key2 ': 'value2','key3' : 'value3'}
s = str(d)
print(s)
#python3 result :{'key1': 'value1', 'key2 ': 'value2', 'key3': 'value3'}
print(type(s))
#python3 result :<class 'str'>
# Delete dictionary given key key The corresponding value , The return value is the deleted value .key Value must be given .
# otherwise , return default value .
d = {'key1' : 'value1', 'key2 ': 'value2','key3' : 'value3'}
d1 = d.pop('key1')
print(d1)
#python3 result :value1
Two simple exercises
# Save the word in the string and the number of times the word appears in the dictionary
s = 'life is short we need python life is short we need python'
l=s.split(' ')
Dic={}
for item in l:
ret = Dic.get(item)
if ret == None:
Dic[item] = 1
else:
Dic[item] += 1
print(Dic)
#python3 result :{'life': 2, 'is': 2, 'short': 2, 'we': 2, 'need': 2, 'python': 2}

Dictionaries support infinite nesting

dic1 = {'Tom':{'A':[1,2,3],'B':[4,5,6]},'Lili':{'D':[7,8,9],'E':['A','B','C','D']}}
for i in dic1['Tom']:
print(i,end=' ')
#python3 result :A B
for i in dic1['Lili']['D']:
print(i,end=' ')
#python3 result :7 8 9

Welcome to xiaotinger's blog :https://blog.csdn.net/u010986753

DB Written interview history link

http://mp.weixin.qq.com/s/Vm5PqNcDcITkOr9cQg6T7w

● The author of this article : Xiaoting'er

● Author's blog address :https://blog.csdn.net/u010986753

● copyright , Welcome to share this article , Reprint please keep the source


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