>>> Dict1 = {' A ': ' 1 ', ' B ': ' 2 ', ' C ': ' 3 ', ' d ': ' 4 '}
>>> print (' A's value is: ', dict1[' a '])
The value of a is: 1
>>> Dict4 = dict (' I ' = ' happy ', you = ' sadness ')
Syntaxerror:keyword can ' t be an expression
>>> dict4[' you '] = ' change sadness '
>>> Dict4
{' I ': ' Happy ', ' you ': ' Change Sadness '}
>>> dict4[' he '] = ' Add one '
>>> Dict4
{' I ': ' Happy ', ' you ': ' Change sadness ', ' he ': ' Add one '}
Fromkeys (S[,v]) creates and returns a new dictionary
>>> Dict1 = {}
>>> Dict1.fromkeys ((+))
{1:none, 2:none, 3:none}
>>> Dict1.fromkeys ((+), ' num ')
{1: ' num ', 2: ' Num ', 3: ' Num '}
>>> Dict1.fromkeys ((1,3), ' number ') # If you want to change the value of the key with Fromkeys, the new dictionary will not be created successfully
{1: ' Number ', 3: ' Number '}
Ways to access a dictionary
Keys ()
VALUES ()
Items ()
>>> dict2 = {}
>>> Dict2 = Dict2.fromkeys (range (2), ' the ' only ')
>>> Dict2
{0: ' the ' ", 1: ' The '"}
>>> for Eachkey in Dict2. keys (): #values as ' an '
Print (Eachkey)
0
1
>>> for Eachitem in Dict2. items ():
Print (Eachitem)
(0, ' a few ')
(1, ' a few ')
Get () Find Method
>>> Dict2 = Dict2.fromkeys (range (10), ' the ' only ')
>>> Dict2
{0: ' A few ', 1: ' The ', ' 2: ' The ', ' 3: ' The ', ' 4: ' The ', ' 5: ' The ', ' 6: ' The ', ' 7
>>> dict2.get (5, ' none ') #如果有则返回值, if not, return ' none '
' A few '
>>> Dict2.get (10, ' none ')
No
>>> 5 in Dict2 #成员操作符判定
True
>>> in Dict2
False
Clean () Empty A dictionary
>>> Dict2.fromkeys (Range (1), ' happy ')
{0: ' Happy '}
>>> Dict2.clear ()
>>> Dict2
{}
Copy ()
>>> a = {1: ' One ', 2: ' One ', 3: ' Three ', 4: ' Four '}
>>> A
{1: ' One ', 2: ' Both ', 3: ' Three ', 4: ' Four '}
>>> B = a.copy ()
>>> C = A
>>> b
{1: ' One ', 2: ' Both ', 3: ' Three ', 4: ' Four '}
>>> C
{1: ' One ', 2: ' Both ', 3: ' Three ', 4: ' Four '}
>>>
>>> c[5] = ' five '
>>> C
{1: ' One ', 2: ' Both ', 3: ' Three ', 4: ' Four ', 5: ' Five '}
>>> A
{1: ' One ', 2: ' Both ', 3: ' Three ', 4: ' Four ', 5: ' Five '}
>>> b
{1: ' One ', 2: ' Both ', 3: ' Three ', 4: ' Four '}
Pop ()
Popitem () Random Popup
>>> A.pop (2)
' Both '
>>> A
{1: ' One ', 3: ' Three ', 4: ' Four ', 5: ' Five '}
>>> A.popitem ()
(5, ' five ')
>>> A
{1: ' One ', 3: ' Three ', 4: ' Four '}
>>> A.setdefault(7, ' seven ')
' Seven '
>>> A
{1: ' One ', 3: ' Three ', 4: ' Four ', 7: ' Seven '}
>>> B = {7: ' SEVEN '}
>>> A
{1: ' One ', 3: ' Three ', 4: ' Four ', 7: ' Seven '}
>>> A.update (b)
>>> A
{1: ' One ', 3: ' Three ', 4: ' Four ', 7: ' SEVEN '}
Python Create and Access dictionaries