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:
- The speed of finding and inserting is very fast and will not slow with the increase of key;
- It takes a lot of memory, and it wastes a lot of memory.
And the list is the opposite:
- The time to find and insert increases as the element increases;
- 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