字典的使用
>>> #字典
>>> names=[‘alice‘,‘beth‘,‘cecil‘,‘dee-dee‘,‘earl‘]
>>> numbers=[‘2341‘,‘885‘,‘529‘,‘458‘,‘123‘]
>>> numbers[names.index(‘cecil‘)]
‘529‘
创建字典:
>>> phonebook={‘alice‘:‘2145‘,‘Bob‘:‘5520‘,‘tom‘:‘7829‘,‘smith‘:‘5691‘}
1.dict函数
>>> items=[(‘names‘,‘gumby‘),(‘number‘,‘1111111111111‘)]
>>> d=dict(items)
>>> d
{‘names‘: ‘gumby‘, ‘number‘: ‘1111111111111‘}
>>> d[‘names‘]
‘gumby‘
另一种创建方式
>>> d=dict(name=‘gumby‘,age=42)
>>> d
{‘age‘: 42, ‘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 is %s. "%(name,labels[key],people[name][key])
alice‘s phone number is 2341.
关于字典的额操作还有些函数,先学后面的知识,等到需要用到字典操作函数时在过来补充。