A Dictionary of Python learning

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 ()
Method Name Operation
Dict.clear () Delete all elements in a dictionary
Dict.copy () Returns a copy of the dictionary (shallow copy)
DICT.FROMKEYSC (Seq,val=none) Creates and returns a new dictionary that is the key to the dictionary with the elements in the SEQ, and Val does the initial value for all the keys in the dictionary (the default is None if this value is not provided)
Dict.get (Key,default=none) The key to the dictionary dict, returns the value it corresponds to, and returns the value of default if the key does not exist in the dictionary (note that the default value of the parameter is None)
Dict.has_key (Key) Returns True if the key (key) exists in the dictionary, otherwise false is returned. After the Python2.2 version introduced in and not, this method is almost obsolete, but still provides a working interface.
Dict.items () Returns a list that contains a tuple (key, value) in the dictionary
Dict.keys () Returns a list containing the keys in the dictionary
Dict.values () Returns a list that contains all the values in the dictionary
Dict.iter () Methods Iteritems (), Iterkeys (), and itervalues () are the same as the non-iterative methods they correspond to, but they return an iteration instead of a list.
Dict.pop (key[, default]) Similar to the method get (), if the key key exists in the dictionary, delete and return Dict[key], the Keyerror exception is thrown if the key key does not exist and the value of default is not given.
Dict.setdefault (Key,default=none) Similar to Method set (), if the key key is not present in the dictionary, it is assigned by Dict[key]=default.
Dict.setdefault (Key,default=none) Similar to Method set (), if the key key is not present in the dictionary, it is assigned by Dict[key]=default.

A Dictionary of Python learning

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.