将值连续匹配到Python中的键和值列表

时间:2013-10-30 12:21:34

标签: python dictionary

我有一个按键列表:

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文件)。

我坚持如何以最有效的方式做到这一点。有什么建议?非常感谢提前!

4 个答案:

答案 0 :(得分: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']}

答案 1 :(得分: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']
}

答案 2 :(得分: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)

列表在此维护订单,最终得到您想要的结构:值每个键,按访问的网页顺序存储在有序列表中。

答案 3 :(得分: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