Python Learning note Four, dict and set

Source: Internet
Author: User

Dict is the abbreviation of the Dictionary dictionary, he holds the key value pair key/value, denoted in curly braces, the format is d={' micheal ':, ' Jack ': 88}

Direct print (d[' micheal ') #输出相应的99 when we visit;

Can be copied to dict and will overwrite the original value, but if the key does not have the corresponding value will be an error,

With the Get method provided by Dict, if key does not exist, you can return none, or your own specified Value:print (d.get (' Tomash ')) #会输出none

In Dict if there is a duplicate key, whichever is the previous key, and automatically filter out the back key, such as

d={' micheal ': The ' micheal ': The ' Jack ': 88};print (d)

d={' micheal ':, ' micheal ': "The", ' Jack ':;p rint (d)

If you want to delete a key, its corresponding value will also be deleted, Pop (key)

[CP] and list comparison, Dict has the following features:

    1. The speed of finding and inserting is very fast and will not slow with the increase of key;
    2. It takes a lot of memory, and it wastes a lot of memory.

And the list is the opposite:

    1. The time to find and insert increases as the element increases;
    2. Small footprint and little wasted memory. So, Dict is a way of exchanging space for TIME [CP]

Set is an order-free data collection that requires a list parameter to be passed in, and can only be a list parameter, if there are two parameters will be error, he is different from dict is no key corresponding to the value of the duplicate elements will be automatically filtered out,

Add the Set element through the Add (key) method and remove the element by removing the key method;

Since set is a collection then two sets of sets can do the intersection and set,

S1=set ([+]);
S2=set ([2,3,4]);
Print (S1&S2);
Print (S1|S2);

A tuple can be a key for set and dict because it is immutable, but if the tuple contains a list, for example:

a=[2,3] #list
d= (' H ', (' a ', a)) #tuple
Dict={d: ' Value '} #dict

Can not be hashed, that is, will be an error, not as a parameter;

Python Learning note Four, dict and set

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.