Getting started with Python-understanding

Source: Internet
Author: User

Dictionary definitions and attribute dictionaries are the only types of mappings in the Python language.

definition:{key1:value1,key2:value2}

1、键与值用冒号“:”分开;2、项与项用逗号“,”分开;

Characteristics:

1.key-value结构2.key必须可hash、且必须为不可变数据类型、必须唯一3.可存放任意多个值、可修改、可以不唯一4.无序
To Create a dictionary: person  = { "name" "mr.wu" ‘age‘ 18 } person  = dict ({ "name" "mr.wu" ‘age‘ 18 })

Common operations:

  • Index
  • New  
          
     1  Dic_t[ " k2  " ] =     Span style= "COLOR: #800000" > " v2  2  Dict.fromkeys ([ " key1   ", "  key2   ", "  key3   "], "  value   " #   loop key list, generate a dictionary, if the following values are not assigned, all are none, if there is an assignment, the value of key is all for this string  

     

  • Delete
    1        Dic_t.pop ("key")       # Deletes the key-value pair and returns the corresponding value2        dic_ T.popitem ()        # randomly delete 3        del dic_t["key "]      

  • Modify
    1 dic_t.update (v2) # Merge two dictionaries, if the same key,list2 overrides the list key
  • Enquiry
    1"Key"inchdic_t#Standard usage2Dic_t.get ("Key")#get Value,key does not exist then return none3dic_t["key] #获取value, key does not exist then error4Dic_t.items ()#convert key and values into a small tuple5Dic_t.setdefault ("Key","Values")#If the list has this key, it gets the value of the key, and if not, creates a key-value pair

  • Key, value, key-value pairs
    1        Dic_t.keys ()      # print all the keys2        dic_t.value ()    # Print all the values 3        Dic_t.items ()    # Print all key-value pairs

  • Cycle
    for key in dic_t: #循环key
    For key,values in Dic_t.items (): #循环键值对
  • Length
enumrate ()Add an ordinal to an object that can be iterated 1 li = [11,22,33] 2 for k,v in Enumerate (LI, 1): 3 print(k,v)

Getting started with Python-understanding

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.