【发布时间】:2018-12-10 07:01:38
【问题描述】:
我有一个列表,其中包含这样的字典:
json_obj = [[{'id': None},{'id': '5b98d01c0835f23f538cdcab'},{'id': '5b98d0440835f23f538cdcad'},{'id': '5b98d0ce0835f23f538cdcb9'}],[{'id': None},{'id': '5b98d01c0835f23f538cd'},{'id': '5b98d0440835f23f538cd'},{'id': '5b98d0ce0835f23f538cdc'}]]
我希望它存储在这样的列表列表中:
y=[['None','5b98d01c0835f23f538cdcab','5b98d0440835f23f538cdcad','5b98d0ce0835f23f538cdcb9'],['None','5b98d01c0835f23f538cd','5b98d0440835f23f538cd','5b98d0ce0835f23f538cdc']]
为了从我尝试过的字典中读取 id
for d in json_obj:
print(d['id'])
但我在上面的代码中看到了这个错误:
TypeError: list indices must be integers or slices, not str
【问题讨论】:
-
您没有包含字典的列表。您有一个列表,其中包含一个包含字典的列表。
标签: python python-3.x list for-loop nested