字典问题

>>> dir1=dir(((1,100),(2,200)))
>>> dir1
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
>>> dir1=dict(((1,100),(2,200)))
>>> dir1
{1: 100, 2: 200}
>>> dir2=(黎明='李宁',大家='你好')
SyntaxError: invalid syntax
>>> dir2=dict(黎明='李宁',大家='你好')
SyntaxError: invalid syntax
>>> dir2=dict(黎明='李宁',大家='你好')
SyntaxError: invalid syntax
>>> dir2=dict(黎明='李宁', 大家='你好')
SyntaxError: invalid syntax
>>> 
>>> 
>>> dir2=dict(    nihao='wozhidao', buha='buzhida')
>>> dir2
{'nihao': 'wozhidao', 'buha': 'buzhida'}
>>> dir2=dict(    nihao='天天', buha='大世界')
>>> 地热
SyntaxError: invalid syntax
>>> 
>>> dir2
{'nihao': 'xccxecxccxec', 'buha': 'xb4xf3xcaxc0xbdxe7'}
>>> dir2=dict(    nihao='wozhidao', 大家='buzhida')
SyntaxError: invalid syntax
>>> dir3['12']=21

Traceback (most recent call last):
  File "<pyshell#37>", line 1, in <module>
    dir3['12']=21
NameError: name 'dir3' is not defined
>>> dir2
{'nihao': 'xccxecxccxec', 'buha': 'xb4xf3xcaxc0xbdxe7'}
>>> dir1
{1: 100, 2: 200}
>>> dir1[1]=2000
>>> dir1
{1: 2000, 2: 200}
>>> dir1[3]=3000
>>> dir1
{1: 2000, 2: 200, 3: 3000}
>>>字典的几种初始化问题,注意汉语。。。。。。。。。如果添加,修改------------------》map






#字典的创建删除一些操作

>>> dir1=dir(((1,100),(2,200))) >>> dir1 ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index'] >>> dir1=dict(((1,100),(2,200)))#注意此处两个外面的小括号 >>> dir1 {1: 100, 2: 200} >>> dir2=(黎明='李宁',大家='你好') SyntaxError: invalid syntax >>> dir2=dict(黎明='李宁',大家='你好') SyntaxError: invalid syntax >>> dir2=dict(黎明='李宁',大家='你好') SyntaxError: invalid syntax >>> dir2=dict(黎明='李宁', 大家='你好') SyntaxError: invalid syntax >>> >>> >>> dir2=dict( nihao='wozhidao', buha='buzhida') >>> dir2 {'nihao': 'wozhidao', 'buha': 'buzhida'} >>> dir2=dict( nihao='天天', buha='大世界') >>> 地热 SyntaxError: invalid syntax >>> >>> dir2 {'nihao': 'xccxecxccxec', 'buha': 'xb4xf3xcaxc0xbdxe7'} >>> dir2=dict( nihao='wozhidao', 大家='buzhida') SyntaxError: invalid syntax >>> dir3['12']=21 Traceback (most recent call last): File "<pyshell#37>", line 1, in <module> dir3['12']=21 NameError: name 'dir3' is not defined >>> dir2 {'nihao': 'xccxecxccxec', 'buha': 'xb4xf3xcaxc0xbdxe7'} >>> dir1 {1: 100, 2: 200} >>> dir1[1]=2000 >>> dir1 {1: 2000, 2: 200} >>> dir1[3]=3000 >>> dir1 {1: 2000, 2: 200, 3: 3000} >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> dir11={} >>> >>> dir11 {} >>> dir11.fromkeys((1,2,3)) {1: None, 2: None, 3: None} >>> dir11.fromkeys((1,2,3),('number')) {1: 'number', 2: 'number', 3: 'number'} >>> dir11.fromkeys((1,2,3),('one','two','three')) {1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')} >>> dir11.fromkeys((1,3),'nukmber') {1: 'nukmber', 3: 'nukmber'} >>> >>> >>> #eg2 >>> di={(1,11),(2,22),3(33)} Traceback (most recent call last): File "<pyshell#63>", line 1, in <module> di={(1,11),(2,22),3(33)} TypeError: 'int' object is not callable >>> di=dirt{(1,11),(2,22),3(33)} SyntaxError: invalid syntax >>> di=dict{(1,11),(2,22),3(33)} SyntaxError: invalid syntax >>> di=dict{(1,11),(2,22),(3,33)} SyntaxError: invalid syntax >>> di=dirt{1:11,2:22,3:33} SyntaxError: invalid syntax >>> di=dict{1:11,2:22,3:33} SyntaxError: invalid syntax >>> >>> di=dict(((1,11),(2,22),3(33))) Traceback (most recent call last): File "<pyshell#70>", line 1, in <module> di=dict(((1,11),(2,22),3(33))) TypeError: 'int' object is not callable >>> di=dict(((1,11),(2,22),(3,33))) >>> di {1: 11, 2: 22, 3: 33} >>> di.iterkeys <built-in method iterkeys of dict object at 0x0000000002E498C8> >>> list(di.iterkeys) Traceback (most recent call last): File "<pyshell#74>", line 1, in <module> list(di.iterkeys) TypeError: 'builtin_function_or_method' object is not iterable >>> for eachKey in di.iterkeys: print eachKey Traceback (most recent call last): File "<pyshell#77>", line 1, in <module> for eachKey in di.iterkeys: TypeError: 'builtin_function_or_method' object is not iterable >>> for eachKey in di.iterkeys():#三种变量方式 1遍历值 2遍历建 3遍历键值对 print(eachKey) 1 2 3 >>> for eachKey in di.iterkeys: print eachKey Traceback (most recent call last): File "<pyshell#84>", line 1, in <module> for eachKey in di.iterkeys: TypeError: 'builtin_function_or_method' object is not iterable >>> for eachKey in di.itervalues(): print(eachKey) 11 22 33 >>> for eachitem in di.iteritems SyntaxError: invalid syntax >>> for eachitem in di.iteritems(): print (eachitem)


原文地址:https://www.cnblogs.com/13224ACMer/p/6048744.html