Python 打印队列字典
如何打印队列字典而不弹出内容? 我有这样的想法:Python 打印队列字典,python,dictionary,queue,Python,Dictionary,Queue,如何打印队列字典而不弹出内容? 我有这样的想法: >>> mac = '\x04\xab\x4d' >>> mydict = {} >>> if mac in mydict.keys(): ... mydict[mac].append("test") ... else: ... mydict[mac]=[] ... mydict[mac].append("test") ... >>> >>
>>> mac = '\x04\xab\x4d'
>>> mydict = {}
>>> if mac in mydict.keys():
... mydict[mac].append("test")
... else:
... mydict[mac]=[]
... mydict[mac].append("test")
...
>>>
>>> for key,val in mydict.Items():
... print key
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'Items'
>>> for key,val in mydict.Item():
... print key
mac='\x04\xab\x4d'
>>>mydict={}
>>>如果mac在mydict.keys()中:
... mydict[mac]。追加(“测试”)
... 其他:
... mydict[mac]=[]
... mydict[mac]。追加(“测试”)
...
>>>
>>>对于键,mydict.Items()中的val:
... 打印键
...
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“dict”对象没有属性“Items”
>>>对于键,mydict.Item()中的val:
... 打印键
我想知道如何显示我字典的内容
谢谢大家!!
Ron正如@Rohit Jain正确指出的那样:没有
Items()
方法-有Items()
不过(如果您使用的是python 2),通常最好使用生成器方法—使用iteritems()
,iterkeys()
,itervalues()
:
请注意,在Python3中,items()
、keys()
和values()
返回迭代器
另见:
Items()
方法-有Items()
不过(如果您使用的是python 2),通常最好使用生成器方法—使用iteritems()
,iterkeys()
,itervalues()
:
请注意,在Python3中,items()
、keys()
和values()
返回迭代器
另见:
mydict.items()
加小写的i
。谢谢。你可以把这个写下来作为答案,我会检查它的答案。它是mydict.items()
小写i
。谢谢。您可能希望将此作为答案,我将检查它的答案。在Python3.xiter
中,方法被删除(它们现在基本上是默认的),在Python3.xiter
中,方法被删除(它们现在基本上是默认的)
>>> for key,val in mydict.iteritems():
... print key, val
...
�M ['test']
>>> for key in mydict.iterkeys():
... print key
...
�M
>>> for value in mydict.itervalues():
... print value
...
['test']