Python dictionary (Dictionary) usage examples

Source: Internet
Author: User
This example describes the usage of the dictionary (Dictionary) in Python. Share to everyone for your reference. The specific analysis is as follows:

A dictionary (Dictionary) is a data type of a mapping structure that consists of unordered "key-value pairs". The key of a dictionary must be an immutable type, such as a string, a number, a tuple, and a value that can be any Python data type.

1. Create a new dictionary

>>> dict1={} #建立一个空字典 >>> type (DICT1)
 
  

2. Add dictionary elements: two methods

>>> dict1[' A ']=1 #第一种 >>> dict1{' a ': 1} #第二种: SetDefault method >>> Dict1.setdefault (' B ', 2) 2> >> dict1{' A ': 1, ' B ': 2}

3. Delete Dictionary

#删除指定键-value pairs >>> dict1{' a ': 1, ' B ': 2}>>> del dict1[' a '] #也可以用pop方法, Dict1.pop (' a ') >>> dict1{' B ' : 2} #清空字典 >>> dict1.clear () >>> dict1 #字典变为空了 {} #删除字典对象 >>> del dict1>>> Dict1traceback (most recent): File "
 
  
   
  ", line 1, in 
  
   
    
   nameerror:name ' Dict1 ' are not defi Ned
  
   
 
  

4. The method of the dictionary

1) Get (Key,default=none)

Returns the value of the key value key, or the value of the default parameter if key is not in the dictionary, which defaults to none

>>> dict1 #空的字典 {}>>> dict1.get (' a ') #键 ' a ' does not exist in Dict1, return none>>> dict1.get (' D1 ', ' No1 ')  #default参数给出值 ' No1 ', so return ' No1 ' No1 ' >>> dict1[' a ']= ' No1 ' #插入一个新元素 >>> dict1{' a ': ' 1111 '}>>> Dict1.get (' a ') #现在键 ' A ' exists, returning its value ' 1111 '

2) Clear Empty Dictionary

3) Has_key (key) Returns True if key appears in Dict, otherwise returns false

>>> dict1{' a ': ' 1111 '}>>> dict1.has_key (' B ') false>>> Dict1.has_key (' a ') True

4) Items Returns a list of Dict (key, value) tuple pairs

>>> dict1{' A ': ' No1 ', ' B ': ' 2222 '}>>> dict1.items [(' A ', ' No1 '), (' B ', ' 2222 ')]

5) Keys returns the list of keys for Dict

6) Values return to Dict list

>>> dict1{' A ': ' No1 ', ' B ': ' 2222 '}>>> dict1.keys () [' A ', ' B ']>>> dict1.values () [' No1 ', ' 2222 ']

7) SetDefault (Key,default=none)

If there is a key in the Dict, the key value is returned, if the key is not found, then the key is added to the dict, the value is given by the default parameter, none

8) Update (DICT2)

Add the Dict2 element to the dict and overwrite the key value in the dict when the key is repeated

>>> dict2{' C ': ' 3333 ', ' B ': ' No2 '}>>> dict1 #dict2和dict1的键 ' B ' repeat {' a ': ' No1 ', ' B ': ' 2222 '}>>> Dict1.update (DICT2) #调用update后, Dict1 's key ' B ' value is overwritten >>> dict1{' a ': ' No1 ', ' C ': ' 3333 ', ' B ': ' NO2 '}

9) Popitem Delete any key-value pairs and return the key-value pair, such as Null in the dictionary, generates an exception

>>> dict1{' B ': ' No2 '}>>> dict1.popitem (' B ', ' NO2 ') >>> dict1{}>>> Dict1.popitem () Traceback (most recent call last): File "
 
  
   
  ", line 1, in 
  
   
    
   keyerror: ' Popitem (): Dictionary is EM Pty '
  
   
 
  

Pop (key,[d]) deletes the key-value pair of the specified key and returns the value corresponding to the key

>>> dict1{' A ': ' No1 ', ' C ': ' 3333 ', ' B ': ' No2 '}>>> dict1.pop (' a ') ' No1 ' >>> dict1{' C ': ' 3333 ', ' B ': ' NO2 '}

Copy returns a shallow copy of the dictionary

Hopefully this article will help you with Python programming.

  • 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.