b={ 'cricket': { 'Arts & Entertainment': [{'id': '6003760631113'},
{'id': '6003350271605'},
{'id': '6003106646578'},
{'id': '6003252371486'},
{'id': '6003370637135'}]},
'News, Media & Publications': [ {'id': '6003020834693'},
{'id': '6003139266461'},
{'id': '6003716669862'},
{'id': '6002971085794'},
{'id': '6003172932634'}]
}
如何解析这个嵌套字典,以便检索这个嵌套列表中的所有id ?
我已经试过了:
for j in b['cricket']:
print(j)
给了我这样的结果:-
艺术,娱乐
然后我试了这个:
for j in b['cricket']:
for k in j:
print(k['id'])
给了我这个错误:-误差
ids = [int(row['id']) for row in b['cricket']['Arts & Entertainment']]
会给你一个数字列表,
[6003760631113, 6003350271605, 6003106646578, 6003252371486, 6003370637135]
和你的新编辑;
ids = [int(row['id']) for row in b['cricket']['Arts & Entertainment'] + b['News, Media & Publications']]
试试这个
for i in b['cricket']['Arts & Entertainment']:
print(i['id'])
6003760631113
6003350271605
6003106646578
6003252371486
6003370637135