14. Summary of methods in the Python dictionary

Source: Internet
Author: User

‘‘‘
1. Access, modify, delete the values in the dictionary:
‘‘‘

Dict={' a ': ' One ', ' B ': ' A ', ' C ': ' + ', ' d ': ' 44 '}
Print dict[' A '],dict[' d '] #访问
dict[' B ']= ' abc ' #修改
Print Dict
#删除
Del dict[' C '] #删除字典中的某个值
Print Dict
Dict.clear () #清空字典
Print Dict
Del dict #删除字典
---------------------------------------------

11 44
{' A ': ' One ', ' C ': ' + ', ' B ': ' abc ', ' d ': ' 44 '}
{' A ': ' One ', ' B ': ' abc ', ' d ': ' 44 '}
{}

-----------------------------------------------------------------------------------
‘‘‘
2.len (): Calculates the total number of elements in the dictionary, that is, the total number of keys
‘‘‘
Dict={' a ': ' One ', ' B ': ' A ', ' C ': ' + ', ' d ': ' 44 '}
Print Len (dict)
---------------------------------------------

4

-----------------------------------------------------------------------------------
‘‘‘
3.fromkeys (seq,value): The function is used to create a new dictionary, which is the key for the dictionary of elements in the sequence SEQ, and value is the initial value corresponding to all the keys of the dictionary.
‘‘‘
Seq={' a ': ' One ', ' B ': ' A ', ' C ': ' + ', ' d ': ' 44 '}
Dict=dict.fromkeys (seq)
Print Dict
Print Dict.fromkeys (seq,20)
---------------------------------------------

{' A ': none, ' C ': None, ' B ': none, ' d ': none}
{' A ': +, ' C ': +, ' B ': +, ' d ': 20}

-----------------------------------------------------------------------------------
‘‘‘
4.get (): Returns the value of the specified key, if the value is not in the dictionary, returns the default value
SetDefault (): The function and the Get () method are similar, if the key does not exist in the dictionary, the key is added and the value is set to the default value.
‘‘‘
Seq={' a ': ' One ', ' B ': ' A ', ' C ': ' + ', ' d ': ' 44 '}
Print Seq.get (' a ')
Print Seq.get (' e ')
Print Seq.get (' e ', ' fuck ')
Print seq
Print Seq.setdefault (' e ', ' fuck ')
Print seq
---------------------------------------------

11
None
Fuck
{' A ': ' One ', ' C ': ' + ', ' B ': ' + ', ' d ': ' 44 '}
Fuck
{' A ': ' One ', ' C ': ' A ', ' B ': ' A ', ' e ': ' Fuck ', ' d ': ' 44 '}

-----------------------------------------------------------------------------------
‘‘‘
5.has_key (): Used to determine if the key exists in the dictionary, or False if the key returns true in the dictionary dict.
‘‘‘
Seq={' a ': ' One ', ' B ': ' A ', ' C ': ' + ', ' d ': ' 44 '}
Print Seq.has_key (' a ')
Print Seq.has_key (' m ')
---------------------------------------------

True
False

-----------------------------------------------------------------------------------
‘‘‘
6.items (): Returns an array of traversed (key, value) tuples in a list
‘‘‘
Seq={' a ': ' One ', ' B ': ' A ', ' C ': ' + ', ' d ': ' 44 '}
Print Seq.items ()
For k,v in Seq.items ():
Print K,v
---------------------------------------------

[(' A ', ' one '), (' C ', ') ', (' B ', ') ', (' d ', ' 44 ')]
A 11
C 33
b 22
D 44

-----------------------------------------------------------------------------------
‘‘‘
7.keys (): Returns all keys in a dictionary in a list
‘‘‘
Seq={' a ': ' One ', ' B ': ' A ', ' C ': ' + ', ' d ': ' 44 '}
Print Seq.keys ()
---------------------------------------------

[' A ', ' C ', ' B ', ' d ']

-----------------------------------------------------------------------------------
‘‘‘
8.dict.update (DICT2): Update key values in Dict2 to Dict
‘‘‘
Dict={' a ': ' One ', ' B ': ' A ', ' C ': ' + ', ' d ': ' 44 '}
dict1={' AAA ': ' 111 ', ' BBB ': ' 222 '}
Dict.update (Dict1)
Print Dict
---------------------------------------------

{' A ': ' One ', ' C ': ' + ', ' B ': ' + ', ' aaa ': ' 111 ', ' d ': ' + ', ' BBB ': ' 222 '}

-----------------------------------------------------------------------------------
‘‘‘
9.values (): Returns all values in the dictionary as a list
‘‘‘
Dict={' a ': ' One ', ' B ': ' A ', ' C ': ' + ', ' d ': ' 44 '}
Print dict.values ()
---------------------------------------------

[' 11 ', ' 33 ', ' 22 ', ' 44 ']

-----------------------------------------------------------------------------------
‘‘‘
10.pop (Key[,default]): Removes the value of the dictionary given key key, and the return value is the deleted value. The key value must be given. Otherwise, the default value is returned.
‘‘‘
Dict={' a ': ' One ', ' B ': ' A ', ' C ': ' + ', ' d ': ' 44 '}
Print Dict.pop (' a ')
Print Dict.pop (' e ', ' Notall ')
---------------------------------------------

11
Notall

-----------------------------------------------------------------------------------

14. Summary of methods in the Python dictionary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.