1 >>> kk={‘11‘:1,‘22‘:2,‘33‘:3} 2 >>> kk[‘11‘] 3 1 4 >>> kk[1] 5 Traceback (most recent call last): 6 File "<pyshell#2>", line 1, in <module> 7 kk[1] 8 KeyError: 1 9 >>> len(kk) 10 3 11 >>> ‘11‘ in kk 12 True 13 >>> list(kk.keys()) 14 [‘11‘, ‘33‘, ‘22‘] 15 >>> kk.keys() 16 dict_keys([‘11‘, ‘33‘, ‘22‘]) 17 >>> kk[‘22‘]=[‘44‘,‘55‘,‘66‘] 18 >>> kk 19 {‘11‘: 1, ‘33‘: 3, ‘22‘: [‘44‘, ‘55‘, ‘66‘]}
1 >>> del kk[‘11‘] 2 >>> kk 3 {‘33‘: 3, ‘22‘: [‘44‘, ‘55‘, ‘66‘]} 4 >>> kk[‘11‘]=‘ppp‘ 5 >>> kk 6 {‘11‘: ‘ppp‘, ‘33‘: 3, ‘22‘: [‘44‘, ‘55‘, ‘66‘]}
1 >>> kk={‘11‘:1,‘22‘:2,‘33‘:3} 2 >>> list(kk.values()) 3 [1, 3, 2] 4 >>> list(kk.items()) 5 [(‘11‘, 1), (‘33‘, 3), (‘22‘, 2)] 6 >>> kk.get(‘11‘) 7 1 8 >>> kk.get(‘66‘) 9 >>> 10 >>> kk 11 {‘11‘: 1, ‘33‘: 3, ‘22‘: 2} 12 >>> hh={‘44‘:4,‘55‘:5} 13 >>> kk.update(hh) 14 >>> kk 15 {‘11‘: 1, ‘33‘: 3, ‘44‘: 4, ‘22‘: 2, ‘55‘: 5} 16 >>> kk={‘11‘:1,‘22‘:2,‘33‘:3} 17 >>> kk 18 {‘11‘: 1, ‘33‘: 3, ‘22‘: 2} 19 >>> hh={‘22‘:4,‘55‘:5} 20 >>> kk.update(hh) 21 >>> kk 22 {‘11‘: 1, ‘33‘: 3, ‘22‘: 4, ‘55‘: 5} 23 >>> kk={‘11‘:1,‘22‘:2,‘33‘:3} 24 >>> kk 25 {‘11‘: 1, ‘33‘: 3, ‘22‘: 2} 26 >>> hh={‘44‘:2,‘55‘:5} 27 >>> kk.update(hh) 28 >>> kk 29 {‘11‘: 1, ‘33‘: 3, ‘44‘: 2, ‘22‘: 2, ‘55‘: 5}
1 >>> kk={‘11‘:1,‘22‘:2,‘33‘:3} 2 >>> kk.pop(‘22‘) 3 2 4 >>> kk 5 {‘11‘: 1, ‘33‘: 3} 6 >>> kk={} 7 >>> kk[5]=‘555‘ 8 >>> kk 9 {5: ‘555‘}
1 >>> matrix={} 2 >>> matrix[(1,2,3)]=44 3 >>> matrix[(5,8,9)]=99 4 >>> matrix 5 {(5, 8, 9): 99, (1, 2, 3): 44} 6 >>> list(zip([‘1‘,‘2‘,‘3‘],[11,22,33])) 7 [(‘1‘, 11), (‘2‘, 22), (‘3‘, 33)] 8 >>> dict(zip([‘1‘,‘2‘,‘3‘],[11,22,33])) 9 {‘1‘: 11, ‘3‘: 33, ‘2‘: 22} 10 >>> hh={k:v for (k,v) in zip([‘a‘,‘b‘,‘c‘],[11,22,33])} 11 >>> hh 12 {‘a‘: 11, ‘c‘: 33, ‘b‘: 22} 13 >>> uu={x:x**3 for x in range(1,5)} 14 >>> uu 15 {1: 1, 2: 8, 3: 27, 4: 64} 16 >>> gg=dict.fromkeys([‘a‘,‘b‘,‘c‘],0) 17 >>> gg 18 {‘a‘: 0, ‘c‘: 0, ‘b‘: 0} 19 >>> gg=dict.fromkeys([‘a‘,‘b‘,‘c‘]) 20 >>> gg 21 {‘a‘: None, ‘c‘: None, ‘b‘: None}
1 >>> dd=dict(a=1,b=2,c=3) 2 >>> dd 3 {‘a‘: 1, ‘c‘: 3, ‘b‘: 2} 4 >>> dd.keys() 5 dict_keys([‘a‘, ‘c‘, ‘b‘]) 6 >>> n=dd.keys() 7 >>> n 8 dict_keys([‘a‘, ‘c‘, ‘b‘]) 9 >>> dd.keys()|{‘b‘,‘c‘,‘d‘} 10 {‘a‘, ‘c‘, ‘b‘, ‘d‘} 11 >>> dd.keys()&{‘b‘,‘c‘,‘d‘} 12 {‘c‘, ‘b‘} 13 >>> dd.has_key(‘b‘) #has_key() 无效了 14 Traceback (most recent call last): 15 File "<pyshell#7>", line 1, in <module> 16 dd.has_key(‘b‘) 17 AttributeError: ‘dict‘ object has no attribute ‘has_key‘
时间: 2024-10-17 19:40:54