2017-03-20 9 views
2
私は、キー=データ内のすべてのリスト(値)と月のリストをビュン後にリストを取得したい

のリストにリストジップHOWTOは:dicts

例えば[(キスム、月/ 2012 、3355)、(キスム、2月/ 2012,3711)、(キスム、傷/ 2012,3517)、(キスム、6月/ 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 i =リスト内の辞書の数を反復しようとしました

list(zip(year,[prices[1] for prices in dataprice[i]['data']])) 

答えて

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'])] 
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の場合は、データキーの第1インデックス(リスト)と月リストのすべての要素を圧縮します。そして、前に付加するために各dataprice

print [[(i['name'],a,b)for a,b in zip(months,[j[1] for j in i['data']])] for i in dataprice] 

出力用nameキーの値を持つこと:

[[('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)]] 

はそれが役に立てば幸い!