Python basics-dictionaries and collections

Source: Internet
Author: User

Python basics-dictionaries and collections
The data structure referenced by the dictionary value is called ing. The dictionary is the only built-in ing type in Python. dict = {key1: value2, key2: value2 ,...} Note: 1. separate keys and values with the colon ":". 2. Separate items with commas (,). 3. Keys in the dictionary must be unique, the value can be unique: dict. get (key, 0) # Same as dict [key]. If no key is added, the default value 0 is returned. [] If not, an exception is thrown. dict. has_key (key) # returns TRUE if this key exists; otherwise, FALSE dict. keys () # Return the dictionary Key List dict. values () # Return the value in the dictionary in the form of a list. The returned value list can contain the repeated element dict. items () # returns all dictionary items in the list. Each item in the list comes from (Key, value), but there is no special order of dict when the items are returned. update (dict2) # Add the merged dictionary dict. popitem () # obtain a pair and delete it from the dictionary. If it is null, an exception dict is thrown. clear () # clear the dictionary, same as del dict. copy () # copy the dictionary dict. cmp (dict1, dict2) # Compare the dictionary, (priority is the number of elements, key size, key value size) the first big return 1, small return-1, returns the copy dict1 = dict # Alias dict2 = dict for 0 dict. copy () # clone, that is, another copy. Loop: traverse the key value for key dict. keys (): print key traverses the value for valuein dict. values (): print value traversal element for item in dict. items (): print itemfor k, v in dict. items (): print k, v judge dict. haskey ('name') has-> True, none-> Falsein, not in 'name' in dict has-> True, no-> False the difference between dictionary and list dict: -The search and insertion speed is extremely fast and will not increase with the increase of keys.-a large amount of memory is required, resulting in a high waste of memory. -KEY immutable-default unordered list:-The time for searching and inserting increases with the increase of elements;-the space occupied is small and the memory is wasted is very small. -Subscripts query-Ordered SET set features: unordered elements do not repeat function: relational test, deduplication a = set ([,]) B = SET, 5, 6]) a & B # intersection. intersection (B), a | B # Calculate the Union set. union (B) a-B # evaluate the difference set. difference (B) a ^ B # returns the symmetric difference set. symmetric_difference (B). issubset (B) # a is a subset of B. issuperset (B) # whether a contains B

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.