我希望在使用key = data
中的每个列表(值)压缩月份列表后获取列表例如 [(kisumu,Jan / 2012,3355),(kisumu,feb / 2012,3711),(kisumu,mar / 2012,3517),(kisumu,jun / 2012,2922)...... ..)
['Jan/2012', 'Feb/2012', 'Mar/2012', 'Apr/2012', 'May/2012', 'Jun/2012', 'Jul/2012', 'Aug/2012', 'Sep/2012', 'Oct/2012', 'Nov/2012', 'Dec/2012']
[{'data': [[1325376000000, 3355], [1328054400000, 3711], [1330560000000, 3517], [1333238400000, 2929], [1335830400000, 3237], [1338508800000, 3397], [1341100800000, 2465], [1343779200000, 2885], [1346457600000, 3343], [1349049600000, 3385], [1351728000000, 4688], [1354320000000, 4401]],
'name': 'Kisumu'},
{'data': [[1325376000000, 4279], [1328054400000, 4557], [1330560000000, 4093], [1333238400000, 3223], [1335830400000, 4121], [1338508800000, 4103], [1341100800000, 3441], [1343779200000, 3047], [1346457600000, 3168], [1349049600000, 3138], [1351728000000, 3197], [1354320000000, 3158]],
'name': 'Eldoret'},
{'data': [[1325376000000, 2732], [1328054400000, 3775], [1330560000000, 4121], [1333238400000, 2849], [1335830400000, 3946], [1338508800000, 4433], [1341100800000, 3054], [1343779200000, 3530], [1346457600000, 3540], [1349049600000, 3675], [1351728000000, 4821], [1354320000000, 4384]],
'name': 'Ktl'},
{'data': [[1325376000000, 5103], [1328054400000, 5112], [1330560000000, 4813], [1333238400000, 3902], [1335830400000, 3799], [1338508800000, 4279], [1341100800000, 3543], [1343779200000, 2663], [1346457600000, 2178], [1349049600000, 2712], [1351728000000, 4213], [1354320000000, 5029]],
'name': 'Nairobi'},
{'data': [[1325376000000, 2843], [1330560000000, 95], [1333238400000, 3583], [1335830400000, 3238], [1338508800000, 3441], [1341100800000, 2864], [1343779200000, 2608], [1346457600000, 2515], [1349049600000, 3035], [1351728000000, 4685], [1354320000000, 4575]],
'name': 'Mombasa'}]
我尝试使用i =列表中的dicts数量进行迭代
list(zip(year,[prices[1] for prices in dataprice[i]['data']]))
答案 0 :(得分:0)
如果最后需要一个列表列表,您可以遍历 dataprice 并在每个数据列表中压缩年份并构建元组:
[[(d['name'], date, x[1]) for date, x in zip(year, d['data'])] for d in dataprice]
#[[('Kisumu', 'Jan/2012', 3355),
# ('Kisumu', 'Feb/2012', 3711),
# ('Kisumu', 'Mar/2012', 3517),
# ('Kisumu', 'Apr/2012', 2929),
# ('Kisumu', 'May/2012', 3237),
# ('Kisumu', 'Jun/2012', 3397),
# ('Kisumu', 'Jul/2012', 2465),
# ('Kisumu', 'Aug/2012', 2885),
# ('Kisumu', 'Sep/2012', 3343),
# ('Kisumu', 'Oct/2012', 3385),
# ('Kisumu', 'Nov/2012', 4688),
# ('Kisumu', 'Dec/2012', 4401)],
# ...
如果你需要一个扁平列表,将内部for循环移动到列表理解的末尾:
[(d['name'], date, x[1]) for d in dataprice for date, x in zip(year, d['data'])]
答案 1 :(得分:0)
输入:
months = ['Jan/2012', 'Feb/2012', 'Mar/2012', 'Apr/2012', 'May/2012', 'Jun/2012', 'Jul/2012', 'Aug/2012', 'Sep/2012', 'Oct/2012', 'Nov/2012', 'Dec/2012']
dataprice =[{'data': [[1325376000000, 3355], [1328054400000, 3711], [1330560000000, 3517], [1333238400000, 2929], [1335830400000, 3237], [1338508800000, 3397], [1341100800000, 2465], [1343779200000, 2885], [1346457600000, 3343], [1349049600000, 3385], [1351728000000, 4688], [1354320000000, 4401]],
'name': 'Kisumu'},
{'data': [[1325376000000, 4279], [1328054400000, 4557], [1330560000000, 4093], [1333238400000, 3223], [1335830400000, 4121], [1338508800000, 4103], [1341100800000, 3441], [1343779200000, 3047], [1346457600000, 3168], [1349049600000, 3138], [1351728000000, 3197], [1354320000000, 3158]],
'name': 'Eldoret'},
{'data': [[1325376000000, 2732], [1328054400000, 3775], [1330560000000, 4121], [1333238400000, 2849], [1335830400000, 3946], [1338508800000, 4433], [1341100800000, 3054], [1343779200000, 3530], [1346457600000, 3540], [1349049600000, 3675], [1351728000000, 4821], [1354320000000, 4384]],
'name': 'Ktl'},
{'data': [[1325376000000, 5103], [1328054400000, 5112], [1330560000000, 4813], [1333238400000, 3902], [1335830400000, 3799], [1338508800000, 4279], [1341100800000, 3543], [1343779200000, 2663], [1346457600000, 2178], [1349049600000, 2712], [1351728000000, 4213], [1354320000000, 5029]],
'name': 'Nairobi'},
{'data': [[1325376000000, 2843], [1330560000000, 95], [1333238400000, 3583], [1335830400000, 3238], [1338508800000, 3441], [1341100800000, 2864], [1343779200000, 2608], [1346457600000, 2515], [1349049600000, 3035], [1351728000000, 4685], [1354320000000, 4575]],
'name': 'Mombasa'}]
对于数据价格中的每个i
,请压缩数据键(列表)和月份列表中第一个索引处的每个元素。然后,为它添加每个数据表的name
键中的值
print [[(i['name'],a,b)for a,b in zip(months,[j[1] for j in i['data']])] for i in dataprice]
输出:
[[('Kisumu', 'Jan/2012', 3355), ('Kisumu', 'Feb/2012', 3711), ('Kisumu', 'Mar/2012', 3517), ('Kisumu', 'Apr/2012', 2929), ('Kisumu', 'May/2012', 3237), ('Kisumu', 'Jun/2012', 3397), ('Kisumu', 'Jul/2012', 2465), ('Kisumu', 'Aug/2012', 2885), ('Kisumu', 'Sep/2012', 3343), ('Kisumu', 'Oct/2012', 3385), ('Kisumu', 'Nov/2012', 4688), ('Kisumu', 'Dec/2012', 4401)], [('Eldoret', 'Jan/2012', 4279), ('Eldoret', 'Feb/2012', 4557), ('Eldoret', 'Mar/2012', 4093), ('Eldoret', 'Apr/2012', 3223), ('Eldoret', 'May/2012', 4121), ('Eldoret', 'Jun/2012', 4103), ('Eldoret', 'Jul/2012', 3441), ('Eldoret', 'Aug/2012', 3047), ('Eldoret', 'Sep/2012', 3168), ('Eldoret', 'Oct/2012', 3138), ('Eldoret', 'Nov/2012', 3197), ('Eldoret', 'Dec/2012', 3158)], [('Ktl', 'Jan/2012', 2732), ('Ktl', 'Feb/2012', 3775), ('Ktl', 'Mar/2012', 4121), ('Ktl', 'Apr/2012', 2849), ('Ktl', 'May/2012', 3946), ('Ktl', 'Jun/2012', 4433), ('Ktl', 'Jul/2012', 3054), ('Ktl', 'Aug/2012', 3530), ('Ktl', 'Sep/2012', 3540), ('Ktl', 'Oct/2012', 3675), ('Ktl', 'Nov/2012', 4821), ('Ktl', 'Dec/2012', 4384)], [('Nairobi', 'Jan/2012', 5103), ('Nairobi', 'Feb/2012', 5112), ('Nairobi', 'Mar/2012', 4813), ('Nairobi', 'Apr/2012', 3902), ('Nairobi', 'May/2012', 3799), ('Nairobi', 'Jun/2012', 4279), ('Nairobi', 'Jul/2012', 3543), ('Nairobi', 'Aug/2012', 2663), ('Nairobi', 'Sep/2012', 2178), ('Nairobi', 'Oct/2012', 2712), ('Nairobi', 'Nov/2012', 4213), ('Nairobi', 'Dec/2012', 5029)], [('Mombasa', 'Jan/2012', 2843), ('Mombasa', 'Feb/2012', 95), ('Mombasa', 'Mar/2012', 3583), ('Mombasa', 'Apr/2012', 3238), ('Mombasa', 'May/2012', 3441), ('Mombasa', 'Jun/2012', 2864), ('Mombasa', 'Jul/2012', 2608), ('Mombasa', 'Aug/2012', 2515), ('Mombasa', 'Sep/2012', 3035), ('Mombasa', 'Oct/2012', 4685), ('Mombasa', 'Nov/2012', 4575)]]
希望它有所帮助!