1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| In [40]: dict = {}
In [41]: type(dict) Out[41]: dict
In [47]: a = {'a':1,'b':2,1:2}
In [48]: a Out[48]: {1: 2, 'a': 1, 'b': 2}
In [49]: a = {'a':1,'b':2,1:2,(1,2):'a'}
In [50]: a Out[50]: {1: 2, 'a': 1, 'b': 2, (1, 2): 'a'}
In [51]: a = {'a':1,'b':2,1:2,(1,2):'a',[1,2,3]:'sf'} --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-51-f813a08c6933> in <module>() ----> 1 a = {'a':1,'b':2,1:2,(1,2):'a',[1,2,3]:'sf'}
TypeError: unhashable type: 'list'
In [112]: a = ['name','age']
In [113]: b = ['Lily','20']
In [114]: zip(a, b) Out[114]: [('name', 'Lily'), ('age', '20')]
In [115]: dict(zip(a,b)) Out[115]: {'age': '20', 'name': 'Lily'}
In [116]: dict(a=10,b=20) Out[116]: {'a': 10, 'b': 20}
In [117]: dict.fromkeys('abc',100) Out[117]: {'a': 100, 'b': 100, 'c': 100}
In [118]: dict.fromkeys(range(1,11),'Tom') Out[118]: {1: 'Tom', 2: 'Tom', 3: 'Tom', 4: 'Tom', 5: 'Tom', 6: 'Tom', 7: 'Tom', 8: 'Tom', 9: 'Tom', 10: 'Tom'}
|