2013-10-30 56 views
0

值鍵和值的列表,我有鍵列表:不斷匹配在Python

Keys=['Description of Supplier:', 'Locally Produced:', 'Imported:', 'Female Managed:', 'Female Owned:', 'Closest Landmark:', '% National Staff:', '% International Staff:', 'Operating since:', 'Previous Name:'] 

我遍歷多個網頁檢索表中的內容爲值和鍵的字典:

webpage1={'Description of Supplier:': 'Hardware, farm tools, articles for office and school supplies (Quincaillerie, outils agricoles, articles pour bureau et articles scolaires)', 'Female Owned:': 'NO', 'Operating since:': '01/1990', 'Female Managed:': 'NO', '% National Staff:': '100', 'Locally Produced:': '100%', 'Previous Name:': ''} 

webpage2={'Description of Supplier:': 'Produce, foods', 'Female Owned:': 'YES', 'Operating since:': '1987', 'Female Managed:': 'NO', '% National Staff:': '80', 'Locally Produced:': '100%', 'Previous Name:': 'Kshop'} 

我想字典的按鍵組合:但是,值必須

newdict={'Description of Supplier:': ['Hardware, farm tools, articles for office and school supplies (Quincaillerie, outils agricoles, articles pour bureau et articles scolaires)','Produce, foods'], 'Female Owned:': ['NO','YES'], 'Operating since:': ['01/1990','1987'], 'Female Managed:': ['NO','NO'], '% National Staff:': ['100','80'], 'Locally Produced:': ['100%','100%] , 'Previous Name:': ['','kshop']} 

按照正確的順序(我將它們寫入一個csv文件)。

我被困在如何以最有效的方式做到這一點。有什麼建議麼?非常感謝!

回答

1

使用collections.defaultdict

from collections import defaultdict 

newdict = defaultdict(list) 
for webpage in (webpage1, webpage2): 
    for key, value in webpage1.items(): 
     newdict[key].append(value) 

newdict = dict(newdict) 

newdict

{'% National Staff:': ['100', '80'], 
'Description of Supplier:': ['Hardware, farm tools, articles for office and school supplies (Quincaillerie, outils agricoles, articles pour bureau et articles scolaires)', 
           'Produce, foods'], 
'Female Managed:': ['NO', 'NO'], 
'Female Owned:': ['NO', 'YES'], 
'Locally Produced:': ['100%', '100%'], 
'Operating since:': ['01/1990', '1987'], 
'Previous Name:': ['', 'Kshop']} 
0

我會使用一個collections.defaultdict object

from collections import defaultdict 

webpage_info = defaultdict(list) 

for webpage in webpages: 
    # collect information on each key: 
    webpage_info[specific_key].append(value_for_this_webpage) 

名單維持秩序在這裏,你最終風趣h您所需的結構:每個密鑰的值,按訪問的網頁順序存儲在有序列表中。

1
data = [webpage1, webpage2] 
newdict = {} 
for currentDict in data: 
    for k, v in currentDict.items(): 
     newdict.setdefault(k, []) 
     newdict[k].append(v) 
print newdict 

輸出

{ 
    'Description of Supplier:': ['Hardware, farm tools, articles for office and school supplies (Quincaillerie, outils agricoles, articles pour bureau et articles scolaires)', 'Produce, foods'], 
    'Female Owned:': ['NO', 'YES'], 
    'Operating since:': ['01/1990', '1987'], 
    'Female Managed:': ['NO', 'NO'], 
    '% National Staff:': ['100', '80'], 
    'Locally Produced:': ['100%', '100%'], 
    'Previous Name:': ['', 'Kshop'] 
} 
0

假設你有網頁的列表,其中每個網頁是一個dict類型的對象,

newdict = {} 

for key in key_list: 
    value_list = [webpage[key] for webpage in webpage_list if key in webpage] 
    if value_list: 
     newdict[key] = value_list 

print newdict