Python基础教程笔记——字典:当索引不好用时
1: #用列表来实现电话薄2: >>> phoneNumbers = ['213123','542113','543231','974723']3: >>> names=['Ada', 'Bill', 'Candy', 'David']4: >>> phoneNumbers[names.index('Bill')]5: '542113'6: >>> 7: 1: >>> mydict={'ada':'1111', 'Bill':'2222', 'Candy':'3333'}2: >>> mydict['Bill']3: '2222'4: >>> 5: 1: #将序列转换成字典 2: >>> mylst=[('name', 'Bill'),('age', 30)] 3: >>> mydict = dict(mylst) 4: >>> mydict 5: {'age': 30, 'name': 'Bill'} 6: >>> mydict['name'] 7: 'Bill' 8: 9: #通过向dict函数传递参数来创建字典10: >>> mylst = dict(height=183,weight=161,hair='black')11: >>> mylst['hair']12: 'black'13: 14: #通过字典来创建另一个字典15: >>> mylst = dict(mydict)16: >>> mylst17: {'age': 30, 'name': 'Bill'}18: >>> 19: 1: #电话薄 2: people = { 3: 'Bill Gunn': 4: { 5: 'phone': '123456', 6: 'addr': 'Tianjin' 7: }, 8: 'Media Zhang': 9: {10: 'phone': '999999',11: 'addr': 'Beijing'12: }13: }14: #输出内容15: lables = {16: 'phone' : 'phone number',17: 'addr' : 'address'18: }19: #用户名20: name = input('Name:')21: key = input('Phone number(p) or address(a):')22: if key == 'p' : key = 'phone'23: if key == 'a' : key = 'addr'24: if name in people:25: print("%s %s is %s" % (name, lables[key], people[name][key]))26: input('Press enter!')27: 28: >>> 29: Name:Bill Gunn30: Phone number(p) or address(a):a31: Bill Gunn address is Tianjin32: Press enter!33: 1: >>> table = {2: 'Alice' : '457819',3: 'Bill' : '929181',4: 'Candy' : '826213'}5: >>> print("Alice's phone number is %(Alice)s." % table)6: Alice's phone number is 457819.7: >>> 8: 1: #字典清空函数的一般用法 2: >>> mydict = {'Bill':'8289213', 'Candy':'192831'} 3: >>> mydict.clear() 4: >>> mydict 5: {} 6: >>> 7: 8: #字典清空的情况一 9: >>> x = {'Bill':'213121'}10: #y和x指向同一个字典对象11: >>> y = x12: # x 指向一个空字典13: >>> x = {}14: # y 仍然指向原字典15: >>> y16: {'Bill': '213121'}17: >>> 18: 19: #字典清空的情况二20: >>> x = {'Name' : 'Bill'}21: #y也指向字典22: >>> y = x23: #对字典中的项操作24: >>> x['Name'] = 'Candy'25: # y 也跟着变化26: >>> y27: {'Name': 'Candy'}28: #清空字典 x29: >>> x.clear()30: #y也被清空了31: >>> y32: {}33: >>> 34: 1: #copy的用法和效果 2: >>> x = {'Name' : 'Bill', 'Colors': {'red', 'green', 'blue'}} 3: >>> y = x.copy() 4: >>> y 5: {'Colors': ['blue', 'green', 'red'], 'Name': 'Bill'} 6: #删除颜色列表中的 ‘red’ 7: >>> x['Colors'].remove('red') 8: >>> x 9: {'Colors': ['blue', 'green'], 'Name': 'Bill'}10: #y字典中的颜色列表也发生的改变11: >>> y12: {'Colors': ['blue', 'green'], 'Name': 'Bill'}13: >>> 14: 15: 16: #deepcopy的用法和效果17: >>> from copy import deepcopy18: >>> x = {'colors':['red','green','blue'],'name':'Bill'}19: >>> x20: {'colors': ['red', 'green', 'blue'], 'name': 'Bill'}21: #将x深拷贝给y22: >>> y = deepcopy(x)23: >>> y24: {'colors': ['red', 'green', 'blue'], 'name': 'Bill'}25: #修改x26: >>> x['colors'].remove('red')27: >>> x28: {'colors': ['green', 'blue'], 'name': 'Bill'}29: #y没有发生改变30: >>> y31: {'colors': ['red', 'green', 'blue'], 'name': 'Bill'}32: >>> 33: 1: >>> {}.fromkeys(['name','age'])2: {'age': None, 'name': None}3: >>> x.fromkeys(['age'])4: {'age': None}5: >>> 6: 1: >>> x = {'name': 'Bill'} 2: #字典中包含指定的键 3: >>> x.get('name') 4: 'Bill' 5: 6: #字典中不包含指定的键,返回空 7: >>> x.get('age') 8: 9: #为不包含的键指定默认值10: >>> x.get('age', 'N/A')11: 'N/A'12: >>> 13: 1: >>> x 2: {'name': 'Bill'} 3: >>> x.items() 4: dict_items([('name', 'Bill')]) 5: 6: #python3中不再包含iteritems 7: >>> x.iteritems() 8: Traceback (most recent call last): 9: File "<pyshell#66>", line 1, in <module>10: x.iteritems()11: AttributeError: 'dict' object has no attribute 'iteritems'12: 1: >>> x = {'Name':'Bill', 'Age':'30'}2: >>> x3: {'Age': '30', 'Name': 'Bill'}4: >>> x.keys()5: dict_keys(['Age', 'Name'])6: >>> 7: 1: >>> x = {'a':1,'b':2}2: >>> x3: {'a': 1, 'b': 2}4: >>> x.pop('a')5: 16: >>> x7: {'b': 2}8: >>> 9: 1: >>> x = {'a': '1', 'c': '3', 'b': '2'}2: >>> while len(x) > 0:3: x.popitem()4: ('a', '1')5: ('c', '3')6: ('b', 2)7: >>> x8: {}9: 1: >>> x = {'a':'1','b':'2','c':'3'} 2: >>> x 3: {'a': '1', 'c': '3', 'b'/span>: '2'} 4: #字典中包含 a,所以返回键a对应的值1 5: >>> x.setdefault('a') 6: '1' 7: #因为字典中有键a,所以setdefault方法不会改变键a对应的值 8: >>> x.setdefault('a', 'A') 9: '1'10: >>> x11: {'a': '1', 'c': '3', 'b': '2'}12: #字典中没有键d,调用setdefault方法时也没有设置默认值,13: #所以调用后,在字典中添加了键为d的键值对儿,并返回空。14: >>> x.setdefault('d')15: >>> x16: {'a': '1', 'c': '3', 'b': '2', 'd': None}17: #调用setdefault时带默认值18: >>> x.setdefault('e', '5')19: '5'20: >>> x21: {'a': '1', 'c': '3', 'b': '2', 'e': '5', 'd': None}22: >>> x.setdefault('d', '4')23: >>> 24: 1: >>> x = {'a':'1','b':'2','c':'3'} 2: >>> x 3: {'a': '1', 'c': '3', 'b': '2'} 4: >>> y = {'a':'A', 'z':'zoo'} 5: >>> x.update(y) 6: #键‘a’被更新到字典中,‘z’被添加到字典中 7: >>> x 8: {'a': 'A', 'c': '3', 'b': '2', 'e': '5', 'd': None, 'z': 'zoo'} 9: >>> z=[('g','11'),('q','12')]10: >>> x.update(z)11: >>> x12: {'a': 'A', 'q': '12', 'c': '3', 'b': '2', 'e': '5', 'd': None, 'g': '11', 'z': 'zoo'}13: >>> 14: 1: >>> x2: {'a': 'A', 'q': '12', 'c': '3', 'b': '2', 'e': '5', 'd': None, 'g': '11', 'z': 'zoo'}3: >>> x.values()4: dict_values(['A', '12', '3', '2', '5', None, '11', 'zoo'])5: >>> 6: Date: 2011-11-21 09:06:22
Org version 7.7 with Emacs version 23
Validate XHTML 1.0