我有這樣的JSON,我想只提取把所有的ID在字典生成列表follower_ids WRT PROJECT_ID到一個列表如下:通過使用python
followers_ids_for_01 = [123,124,...]
followers_ids_for_02 = [125,126,...]
的Json列表:
{
"data": [{
"id": 01,
"followers": [{
"id": 123,
"name": "Shivkumar"
}, {
"id": 124,
"name": "Sneha"
},
{
.....
}],
}, {
"id": 02,
"followers": [{
"id": 125,
"name": "Teja"
}, {
"id": 126,
"name": "Harsha"
},
{
......
}],
}]
}
我試過的:
data =json_list
projects = data['data']
followers_ids =[]
for project in projects:
for i in project['followers']:
followers_ids.append(i['id'])
print followers_ids
輸出:
[123, 124, 125, 126]
有人建議如何產生「followers_ids_for_{project_id}
」列表,並添加其各自的追隨者IDS的呢?
編輯/更新:
我需要生成命名變量空白列表
datalists = dict()
for project in projects:
datalists["followers_ids_for_{}".format(project['id'])] = []
print datalists
#{'followers_ids_for_1': [], 'followers_ids_for_2': []}
您將需要一本字典,否則不能「生成列表」。 –
是的..我已經通過一些其他答案,如https:// stackoverflow。com/a/42768290/6107715我還在尋找一種解決方案,我可以獲得命名空白列表 –
您的意思是,您想要有許多單獨的變量名稱「followers_ids_for_01」,「followers_ids_for_02」等等? –