Python object types--dictionaries and collections

Source: Internet
Author: User

Dictionary

1. The dictionary itself is hashed and unordered, but the collection of keys in the dictionary or the collection of values can be ordered, and a list is returned by keys () or values (), which is sortable. Note: The algorithm of a hash table is to get the key, execute a function called a hash on the key, and, based on the result of the calculation, choose to store your value in an address of the data structure.

2, the creation of the dictionary can be directly assigned value, can be generated through the factory function Dict (), if the parameter is iterative, that is, a sequence or an iterator, or an object that supports iteration, no iterative element must appear in pairs, eg:dict ([[' X ', 1],[' Y ', 2]]) You can also use the built-in method Fromkeys () to create a dictionary in which the elements have the same value. Eg:d={}.fromkeys (Seq,val=none) creates a dictionary with the element in SEQ as the key, and Val for all keys.

3, check whether a dictionary has a key method is to use in

4. The keys in the dictionary must be hashed, that is, the keys are immutable, so numbers and strings can be used as keys, lists, and other dictionaries.

5, dictionary comparison: the first is the size of the dictionary, then the key, and finally the value

6, built-in function ("Python core note" P172): Dict.get (Key,default=none), the key, the corresponding value returned, if the key does not exist, return the default value, so do not have to worry about throwing an exception; Dict.setdefault (key,default=none), check if the dictionary contains a key, if present, return its value, if it does not exist, assign a default value to this key and return this value

7. Dictionary key: One key is not allowed to correspond to multiple values; keys must be hashed, to illustrate that numbers with equal values denote the same key, that is, Integer 1 and float 1.0 represent the same key.

8. The dictionary is used for sparse data structures: You can only assign values where there is value, and other places can not be set. The built-in function get () is available when you want to invoke


Collection

1, the set is divided into mutable sets and immutable sets. Immutable collection members are hashed and are keys that can be used as dictionaries.
Collection Creation Method: Factory function set (), Frozenset ()

2, set type operator: Union (|), Intersection (&), Difference set (-), symmetric difference (^), if the left and right two operands of the same type, the resulting result type is the same, if the two operand types, the resulting type is the same as the left operand.

This article from "Lotus's Thoughts" blog, please be sure to keep this source http://liandesinian.blog.51cto.com/7737219/1543820

Python object types--dictionaries and collections

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.