2015-08-25 87 views
1

我的字典這樣的清單,獲取特定鍵值的Python

a = [{'SegmentName': 'FICO High', 'CohortMonthYear': '01-01-2015', 'DriverValue': '0.921996823602789', 
     'MonthsOnBooks': '1', 
     'CohortQuarterYear': 'Q1 2015', 'PortfolioId': '55dc0e48c95a8a1a2ffb836e', 'DriverName': 'Active rate'}, 
    {'SegmentName': 'FICO High', 'CohortMonthYear': '01-01-2015', 'DriverValue': '0.9123400335106416', 
     'MonthsOnBooks': '2', 'CohortQuarterYear': 'Q1 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'), 
     'DriverName': 'Active rate'}, 
    {'SegmentName': 'FICO High', 'CohortMonthYear': '01-01-2015', 'DriverValue': '0.9024207736261439', 
     'MonthsOnBooks': '3', 'CohortQuarterYear': 'Q1 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'), 
     'DriverName': 'Active rate'}, 
    {'SegmentName': 'FICO High', 'CohortMonthYear': '01-01-2015', 'DriverValue': '0.889915542915732', 
     'MonthsOnBooks': '4', 
     'CohortQuarterYear': 'Q1 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'), 'DriverName': 'Active rate'}, 
    {'SegmentName': 'FICO High', 'CohortMonthYear': '01-01-2015', 'DriverValue': '0.8806580068608421', 
     'MonthsOnBooks': '5', 'CohortQuarterYear': 'Q1 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'), 
     'DriverName': 'Active rate'}, 
    {'SegmentName': 'FICO High', 'CohortMonthYear': '02-01-2015', 'DriverValue': '0.9458702933667549', 
     'MonthsOnBooks': '1', 'CohortQuarterYear': 'Q1 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'), 
     'DriverName': 'Active rate'}, 
    {'SegmentName': 'FICO High', 'CohortMonthYear': '02-01-2015', 'DriverValue': '0.9335318149106486', 
     'MonthsOnBooks': '2', 'CohortQuarterYear': 'Q1 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'), 
     'DriverName': 'Active rate'}, 
    {'SegmentName': 'FICO High', 'CohortMonthYear': '02-01-2015', 'DriverValue': '0.9225337221412799', 
     'MonthsOnBooks': '3', 'CohortQuarterYear': 'Q1 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'), 
     'DriverName': 'Active rate'}, 
    {'SegmentName': 'FICO High', 'CohortMonthYear': '02-01-2015', 'DriverValue': '0.9109689291572624', 
     'MonthsOnBooks': '4', 'CohortQuarterYear': 'Q1 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'), 
     'DriverName': 'Active rate'}, 
    {'SegmentName': 'FICO High', 'CohortMonthYear': '03-01-2015', 'DriverValue': '0.9092991755453589', 
     'MonthsOnBooks': '1', 'CohortQuarterYear': 'Q1 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'), 
     'DriverName': 'Active rate'}, 
    {'SegmentName': 'FICO High', 'CohortMonthYear': '03-01-2015', 'DriverValue': '0.8988101317456371', 
     'MonthsOnBooks': '2', 'CohortQuarterYear': 'Q1 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'), 
     'DriverName': 'Active rate'}, 
    {'SegmentName': 'FICO High', 'CohortMonthYear': '03-01-2015', 'DriverValue': '0.8860809446836048', 
     'MonthsOnBooks': '3', 'CohortQuarterYear': 'Q1 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'), 
     'DriverName': 'Active rate'}, 
    {'SegmentName': 'FICO High', 'CohortMonthYear': '04-01-2015', 'DriverValue': '0.9471495415096017', 
     'MonthsOnBooks': '1', 'CohortQuarterYear': 'Q2 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'), 
     'DriverName': 'Active rate'}, 
    {'SegmentName': 'FICO High', 'CohortMonthYear': '04-01-2015', 'DriverValue': '0.9364314248533585', 
     'MonthsOnBooks': '2', 'CohortQuarterYear': 'Q2 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'), 
     'DriverName': 'Active rate'}, 
    {'SegmentName': 'FICO High', 'CohortMonthYear': '05-01-2015', 'DriverValue': '0.9344317261744861', 
     'MonthsOnBooks': '1', 'CohortQuarterYear': 'Q2 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'), 
     'DriverName': 'Active rate'}] 

我需要遍歷字典的列表,並獲得所有字典的DriverValue,而無需重複分段名和DriverName和PortfolioId。

這是到目前爲止我的代碼,

mm = [] 
for i in a: 
    ss = {'DriverName': i['DriverName'], 'SegmentName': i['SegmentName'], 'PortfolioId': i['PortfolioId'], 
      'DriverValue': []} 

    if all([i['DriverName'] == 'DriverName' and i['SegmentName'] == 'SegmentName']): 
     ss['DriverValue'].append(i['DriverValue']) 

    mm.append(ss) 

print(mm) 

的DriverValue總是在列表爲空。我在哪裏做錯了?

樣本輸出:

{ 
    "PortfolioId" : ("55d4247b119a612af00eff4b"), 
    "DriverName" : "Active rate", 
    "SampleData" : [ 
     30, 
     24, 
     6, 
     9, 
     9, 
     9, 
     9, 
     9, 
     9, 
     9, 
     9, 
     9, 
     9, 
     9, 
     9, 
     9, 
     9, 
     9, 
     9, 
     9, 
     9, 
     9, 
     9, 
     9 
    ] 
} 
+5

[從python詞典列表中獲取值列表](http://stackoverflow.com/q/25040875/2301450)? – vaultah

+0

你認爲,'如果'條件在做什麼? – Daniel

+1

請不要將代碼轉儲到您的問題 - 限制列表的4-5項條目將足以說明您的問題 – scytale

回答

1

的Theres可能是一個更優雅的方式來這一點,但是這將讓你的「drivervalue」鍵每次出現的鍵值對字典中的列表,然後店在列表毫米值:

mm = [] 
for b in a: 
    for key,value in b.items(): 
     if key == "DriverValue": 
      mm.append(value) 

只是打周圍,另一種方法是:

p = [d['DriverValue'] for d in a] 

其中p將是值的列表。希望這可以幫助。