Some in-depth experience of Python dictionary

Source: Internet
Author: User
Tags shallow copy

Python dictionary Some in-depth experience

Python dictionary 3 methods, items (), values (), keys (), returned object not list,3 methods The returned object can be iterated, and the dictionary can be deduced

squares={number:number**2 for number in range(100)}  

CPython's dictionary uses a hash table as the underlying data structure, only the hash object can be used as a dictionary key, the hash object is the entire invariant object, and can be compared with other objects, if an object has __hash__ methods, __eq__ properties, is hash.
If two objects are equal, then their hash values must be equal, if the two hash values are equal, then the object is not necessarily equal, the simple way of understanding is that 1 and 6 modulo 5 results are 1, the hash value is like 1, the object is 1,6. This is a hash conflict. CPython solve this conflict by open addressing method, which is when the conflict occurs, the hash value is stored in the hash table as an empty hash address.
Dictionary copy exists with a shallow copy and a deep copy, a shallow copy copies only a first-level directory, and a deep copy copies all directories

a={1:{‘a‘,‘b‘}}
浅拷贝的情况下,去到键1的值,值是和对象a共享的,操做的时候a里的值也会改变,深拷贝就是将所有的内容都可以复制一边
b=a.copy()浅拷贝
form copy import deepcopy深拷贝
c=deepcopy(a)

Some in-depth experience of Python dictionary

Related Article

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.