0

我有一个键列表:

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

4 回答 4

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']}
于 2013-10-30T12:24:25.793 回答
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']
}
于 2013-10-30T12:24:58.327 回答
0

我会使用一个collections.defaultdict对象

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)

列表在这里保持顺序,最终得到您想要的结构:每个键的值,按访问的网页顺序存储在有序列表中。

于 2013-10-30T12:24:26.550 回答
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
于 2013-10-30T12:36:04.223 回答