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

Dict module of Python notes

編輯:Python

#dict( Dictionaries )
# A dictionary is a collection of unordered objects , Use key value (key-value) For storing data
# With extremely fast query speed
# key (key) Immutable type must be used
# Middle key in the same dictionary (key) Is the only one.

01 Create a dictionary

# You can use dict() perhaps {} Create a dictionary object
#dict() Create an empty dictionary

dt01_1 = dict()
dt01_2 = {
}

#dict(mapping)
dt01_3 = {'a':1,'b':2,'c':3}
#dict(iterable)
# Not available here {} Instead of dict()

lt = [1,2,3]
tt = ('a','b','c')
dt01_4 = dict(zip(tt,lt))
dt01_41 = dict((('a',1),('b',2)))

#dict(**kwargs)
# Not available here {} Instead of dict()
dt01_5 = dict(a=1,b=2)
#fromkeys(iterable, value=None, /), Use iteratible objects to create dictionaries that specify default values
dt01_6 = dict.fromkeys(tt,0)

02 Empty dictionary

#clear(…)

dt02 = dict(a=1,b=2)
dt02.clear()
03 Dictionary copy

#copy(…)

dt03 = dict(a=1,b=2)
dt = dt03.copy()
04 Gets the value of the key

#get(self, key, default=None, /)

dt04 = dict(a=1,b=2)
dt = dt04.get('a')
05 Get all key value pairs of the dictionary

#items(…)

dt05 = dict(a=1,b=2)
dt = dt05.items()
06 Get all keys of the dictionary

#keys(…)

dt06 = dict(a=1,b=2)
dt = dt06.keys()
07 Gets all values of the dictionary

#values(…)

dt07 = dict(a=1,b=2)
dt = dt07.values()
08 Pop up the specified key value pair

#pop(k[,d])

dt08 = dict(a=1,b=2)
dt = dt08.pop('c','wrong')
09 Press LIFO The principle pops up the ancestor of key value pairs

#popitem(self, /)

dt09 = dict(a=1,b=2)
dt = dt09.popitem()
10 Try inserting the value of the specified key , If the key has a return value , If not, insert

#setdefault(self, key, default=None, /)

dt10 = dict(a=1,b=2)
dt = dt10.setdefault('a','3')
11 Update dictionary key value pairs , If it exists, it will be updated , Insert if it doesn't exist

#update(…)

dt11 = dict(a=1,b=2)
dt11_1 = dict(a=0,b=1,c=2)
dt11.update(dt11_1)
12 Use the key to get the value , If not, throw it wrong
dt12 = dict(a=1,b=2)
dt = dt12['a']
13 Add key value pair
dt13 = dict(a=1,b=2)
dt13['c'] = 3
14 Delete key value pair
dt14 = dict(a=1,b=2)
del dt14['a']
del dt14
15 Other operating

dt15 = dict(a=1,b=2,c=3)

  • Get dictionary length
    len(dt15)
  • Get key maximum
    min(dt15)
    max(dt15)

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