How to create a dictionary

Source: Internet
Author: User

In tuples and lists, the elements are accessed by numbers, but sometimes we access data by name or even data structures, and in C + + there is the concept of map, which is mapping, and in Python It also provides built-in mapping types-dictionaries. Mapping is actually a set of key and value and the mapping function between, it is characterized by: key uniqueness, key and value of a one-to-many mapping.

1. Creation of dictionaries

The basic form of a dictionary dic={key1:value1, key2:value2 ...}            Create Mode 1: direct type. dict1={} dict2={' name ': ' Earth ', ' Port ': ' 80 '} Creation Method 2: Use the Factory method Dict, through other mappings (such as dictionaries) or (keys, values) such sequence pairs to build the item      s=[(' name ', ' Earth '), (' Port ', ' + ')] dict2=dict (items) dict1=dict ([' Name ', ' earth '],[' Port ', ' 80 ']) Create mode 3: Use the built-in method Fromkeys () to create the ' default ' dictionary, where the elements in the dictionary have the same value (if not given, the default is None) Dict1={}.fromkeys ((' x ', ' y '), -1) #d ict={' x ':-1, ' Y ': -1} dict2={}.fromkeys ((' x ', ' y ')) #dict2 ={' x ': None, ' Y ': none} 2. Accessing values in the dictionaryThe most common and basic way is to use key to access value A. The Get method of Dict1.get (' name ') that accesses value via key is #也可以直接是dictionary [' Key1 '], but when Key1 does not exist, an error is made; Use Get to return none B. Random access where key values are unordered in the dictionary, using the Popitem method is randomly popping a key-value pair C. Returns a list of all values in a dictionary method values 3. Accessing keys in the dictionaryA. Check for Key1 dictionary.has_key (key1) key1 in Dictionarty Key1 not dictionary B. Returns a list of keys in the dictionary Dictionary.keys () 4. Access key-value pairsA. Traversal mode for R in Dicitonary #r是dictionary中的键值对 B. Modify (update) or add dictionary[key1]=value1 5. DeleteA. Press key to delete del Dictionary[key1] B. Delete and return Dictionary.pop (key1) c. Delete all items dictionary.clear () Del Dictionary 6. SortingSorted (Dic.iteritems (), Key=lambda d:d[1], Reverse=false) Description: The element in the dictionary dic according to D[1] (D[1] is value,d[0] is key, and D no matter, can be changed to a or something) Sort in ascending order, by setting reverse true or false to reverse, and returning the sorted dictionary (the sorted dictionary consists of tuples in the form [(key1,value1), (key2,value2),...], and the original dictionary remains the same) 7. OtherLen (dictionary) #返回字典项个数 Dictionary.item () Dictionary.iteritems ()

How to create a dictionary (RPM)

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.