Python:为什么这不起作用?(迭代非序列)

Den*_*nis 2 python iteration dictionary sequence

我有一个字典,每个键包含一个列表作为值.而且我正在尝试查看列表中的所有项目,让我们说我正在尝试打印所有项目,我写道:

for item in aDict: 
    for item2 in aDict[item]: 
        print item2
Run Code Online (Sandbox Code Playgroud)

这会打印出列表中第一个值的项目,然后它会给出一个错误,说"迭代非序列".为什么这样,我应该怎么做?

提前致谢.

Ste*_*ski 7

您的一个字典值不是列表!


Ben*_*Ben 5

我假设其中一个项目aDict不是序列,字符串,列表,元组等:

>>> aDict = { 'a' : [1, 2, 3,], 'b' : [4, 5, 6,], 'c' : [7, 8, 9,] }
>>> for item in aDict:
...     for item2 in aDict[item]:
...         print item2
...
1
2
3
7
8
9
4
5
6
>>>
Run Code Online (Sandbox Code Playgroud)