NOTES: 1. The dictionary is the only mapping type in Python 2, the dictionary contains two features: Key (keys) and values (value) they are paired, enclosed in curly braces, and many pairs are separated by commas. 3. Dictionaries can be created directly using Dict (), such as Dict1 = Dict ((' F ', ' d '), (' I ', ""), (' s ', (), (') ', (' H ', 104), (' C ', 67)), and the dictionary created in the form of {(' F ', (' s ', 104), (' h ', + +), (' C ', 67)}4, Dict2 = dict (key1 = Value1,key2 = value2), then the final form is: Dict2 = {key2:value2,key1: The dict of Value1}python is disordered. 5. Change a value in the dictionary: dict2[key] = ' value ', insert method similar to test question: 0. When you hear the little friends talking about "mapping", "hashing", "hashing" or "relational arrays," what are they actually talking about? A: Yes, in fact they are talking about our "dictionary", which is a concept! 1. Try to put the data (' F ': +, ' C ': ", ' H ': 104, ' I ': ' Max, ' s ': 115) ' Create a dictionary and access the key ' C ' corresponding value? Method One:
Aaa=dict ((' F ', ' d '), ("C", [+], ("H", 104), (' I ', "115"))
Print (aaa["C"])
Method Two:
aaa={' F ': +, ' C ': "," H ": 104, ' I ':", ' s ': 115}
Print (aaa[' C '])
2. The data enclosed in square brackets ("[]") is called a list, so we call it a dictionary with braces ("{}"), right?
Answer: No
For example:
>>> notadict = {1,2,3,4,5}
>>> type (notadict)
<class ' Set ' >
It is not difficult to see that although we enclose some data in curly braces ("{}"), it does not reflect the mapping of the data, so we create a
It's not a dictionary, it's called ' Set ', and that ' set ' is the thing? Take a look at the No. 027 Lecture | Collection: In my world, you are the only
One!
3. How do you understand that there are some things the dictionary can do, but the "omnipotent" list is hard to achieve (my concubine can't do t_t)?
For example:
>>> brand = [' Li ning ', ' Nike ', ' Adidas ', ' Fish C Studio ']
>>> slogan = [' Everything is possible ', ' Just do it ', ' impossible are nothing ', ' let programming Change the world ']
>>> print (' Fish C Studio ' slogan is: ', Slogan[brand.index (' Fish C Studio ')])
The slogan of the Fish C Studio is: Let programming change the world
List brand, slogan index and relative value is not any relationship, we can see that the only link between the two list, the index number of the same
Elements are related (brand corresponding slogan), so here we through Brand.index (' Fish C Studio ') Such statements, indirect implementation through the brand
The ability to find the corresponding slogan.
This is really an achievable approach, uh ... But the use of it, how much some awkward, efficiency is not high. Moreover, Python is based on simplicity,
It is certainly not satisfying, so we need to have a dictionary of this type of mapping to appear:
>>> Dict1 = {' Li ning ': ' Everything is possible ', ' Nike ': ' Just do it ', ' Adidas ': ' Impossible is nothing ', ' Fish C Studio ': ' Let programming Change the world '}
>>> print (' Fish C Studio ' slogan is: ', dict1[' Fish C Studio ')
The slogan of the Fish C Studio is: Let programming change the world
4. Are they all performing the same thing as the code below? Can you tell the difference?
>>> a = Dict (one=1,two=2,three=3)
>>> B = {' One ': 1, ' both ': 2, ' Three ': 3}
>>> C = dict (Zip ([' One ', ' B ', ' three '],[1,2,3])
>>> d = dict ([' One ', 1), (' Three ', 3)])
>>> D = dict ({' Three ': 3, ' one ': 1, ' One ': 2})
Yes, they're all creating dictionaries: A = Dict (one=1, two=2, three=3), well, I don't see the difference.
The actual operating effect is:
>>> A
{' Both ': 2, ' three ': 3, ' One ': 1}
>>> b
{' Both ': 2, ' three ': 3, ' One ': 1}
>>> C
{' Both ': 2, ' three ': 3, ' One ': 1}
>>> D
{' Both ': 2, ' three ': 3, ' One ': 1}
Indicates that Python's dict () is unordered, that is, Dict's traversal order is inconsistent with the initialization order. Refer to: http://book.douban.com/annotation/23775810/
Python's dict usage: http://blog.csdn.net/wangran51/article/details/8440848
5. Can you extrapolate the code that hit the mosaic section?
Data.split (', ')
Moving hands:
0. Try to use the dictionary features to write an address book program, function
Print (' |‐‐‐ Welcome to Directory program ‐‐‐| ')
Print (' |‐‐‐1: Query contact information ‐‐‐| ')
Print (' |‐‐‐2: Insert new contact ‐‐‐| ')
Print (' |‐‐‐3: Delete existing contact ‐‐‐| ')
Print (' |‐‐‐4: Exit Address Book Program ‐‐‐| ')
contacts = Dict ()
While 1:
instr = Int (input (' \ n Please enter the relevant instruction code: '))
If InStr = = 1:
Name = input (' Please enter contact name: ')
If name in Contacts:
Print (name + ': ' + contacts[name])
Else
Print (' The name you entered is no longer in the address Book! ‘)
If InStr = = 2:
Name = input (' Please enter contact name: ')
If name in Contacts:
Print (' The name you entered already exists in the Address Book ‐‐>> ', end= ')
Print (name + ': ' + contacts[name])
If input (' Modify user profile (yes/no): ') = = ' YES ':
Contacts[name] = input (' Please enter user contact number: ')
Else
Contacts[name] = input (' Please enter user contact number: ')
If InStr = = 3:
Name = input (' Please enter contact name: ')
If name in Contacts:
Del (Contacts[name]) # can also use Dict.pop ()
Else
Print (' The contact you entered does not exist. ‘)
If InStr = = 4:
Break
Print (' |‐‐‐ Thank you for using the Address Book Program ‐‐‐| ')
Little Turtle Python 23rd after lecture exercise--025, dictionary