2013-10-30 3 views
0

에 키와 값 목록에 대한 값과 일치 :는 지속적으로 나는 키의 목록을 가지고 파이썬

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']} 

을 가치가있다 그러나 :

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'} 

나는 키가 사전을 결합하려는 올바른 순서로 (나는 그들을 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 :

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) 

이 목록은 여기 순서 유지하고 결국 :

{'% 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 사용하십시오 재치 원하는 구조 : 키 당 , 방문한 웹 페이지 순서에 따라 정렬 된 목록에 저장. 각 웹 페이지가 dict 타입의 객체 인 웹 페이지의 목록을 가지고 가정

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

,

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 
관련 문제