Use of dictionaries
>>> #字典
>>> names=[' Alice ', ' Beth ', ' Cecil ', ' Dee-dee ', ' Earl ']
>>> numbers=[' 2341 ', ' 885 ', ' 529 ', ' 458 ', ' 123 ']
>>> numbers[names.index (' Cecil ')]
' 529 '
To create a dictionary:
>>> phonebook={' Alice ': ' 2145 ', ' Bob ': ' 5520 ', ' Tom ': ' 7829 ', ' Smith ': ' 5691 '}
1.dict function
>>> items=[(' names ', ' Gumby '), (' Number ', ' 1111111111111 ')]
>>> d=dict (items)
>>> D
{' names ': ' Gumby ', ' number ': ' 1111111111111 '}
>>> d[' names ']
' Gumby '
Another way to create
>>> d=dict (name= ' Gumby ', age=42)
>>> D
{' Age ':, ' name ': ' Gumby '}
>>> #基本字典操作
>>> people={' Alice ': {' phone ': ' 2341 ', ' addr ': ' foo drive 23 '},
' Bob ': {' phone ': ' 4515 ', ' addr ': ' The 5th Road '}}
>>> labels={' phone ': ' Phone number ', ' addr ': ' Address '}
>>> name=raw_input (' Name: ')
Name:alice
>>> request=raw_input (' Phone number (p) or address (a) ')
Phone number (p) or address P
>>> if request== ' P ': key= ' phone '
>>> if name in People:print "%s '%s ' s%s."% (Name,labels[key],people[name][key])
Alice ' s phone number is 2341.
There are also some functions for the amount of the dictionary, which is to be added when the dictionary operation function is needed to learn the knowledge behind.
The fourth lesson of the basic Python tutorial 0121