在列表中打印给定字典键的所有值

use*_*933 5 python dictionary

我有一个字典列表,看起来像这样:

list =[{"id": 1, "status": "new", "date_created": "09/13/2013"}, {"id": 2, "status": "pending", "date_created": "09/11/2013"}, {"id": 3, "status": "closed", "date_created": "09/10/2013"}]
Run Code Online (Sandbox Code Playgroud)

我想要做的是能够打印这个与"id"相关的词典列表中的所有值.如果它只是一个词典,我知道我可以这样做:

print list["id"]
Run Code Online (Sandbox Code Playgroud)

如果它只是一个字典,但我如何为字典列表执行此操作?我试过了:

for i in list:
    print i['id']
Run Code Online (Sandbox Code Playgroud)

但我得到一个错误说

TypeError: string indices must be integers, not str
Run Code Online (Sandbox Code Playgroud)

有人可以帮我一把吗?谢谢!

che*_*ner 11

在代码的某处,您的变量被重新分配了一个字符串值,而不是一个字典列表.

>>> "foo"['id']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: string indices must be integers, not str
Run Code Online (Sandbox Code Playgroud)

否则,您的代码将起作用.

>>> list=[{'id': 3}, {'id': 5}]
>>> for i in list:
...   print i['id']
...
3
5
Run Code Online (Sandbox Code Playgroud)

但关于不使用list名称的建议仍然存在.