2

私は辞書「mydict」を持っています。

{   'a': ['xyz1', 'xyz2'],
    'b': ['xyz3', 'xyz4'],
    'c': ['xyz5'],
    'd': ['xyz6']}

次のコードを使用して、この辞書のすべてのキーと値を出力しようとしています。

for username, details in mydict.iteritems():
    pprint.pprint(username + " " + details)

しかし、次のエラーが発生します。

AttributeError: 'list' object has no attribute 'iteritems'

どんな助けでも大歓迎です。

4

2 に答える 2

1

このコードはあなたの例で動作します

>>> import pprint
>>> mydict = {   'a': ['xyz1', 'xyz2'],
    'b': ['xyz3', 'xyz4'],
    'c': ['xyz5'],
    'd': ['xyz6']}

>>> for username, details in mydict.iteritems():
        pprint.pprint((username, details))


('a', ['xyz1', 'xyz2'])
('c', ['xyz5'])
('b', ['xyz3', 'xyz4'])
('d', ['xyz6'])
于 2013-04-04T08:08:24.850 に答える