3

我需要一点功课帮助。我必须编写一个将几个字典组合成新字典的函数。如果一个键出现多次;新字典中与该键对应的值应该是唯一列表。作为一个例子,这是我到目前为止所拥有的:

f = {'a': 'apple', 'c': 'cat', 'b': 'bat', 'd': 'dog'}
g =  {'c': 'car', 'b': 'bat', 'e': 'elephant'}
h = {'b': 'boy', 'd': 'deer'}
r = {'a': 'adam'}

def merge(*d):
    newdicts={}
    for dict in d:
        for k in dict.items():
            if k[0] in newdicts:
                newdicts[k[0]].append(k[1])
            else:
                newdicts[k[0]]=[k[1]]
    return newdicts

combined = merge(f, g, h, r)
print(combined)

输出如下所示:

{'a': ['apple', 'adam'], 'c': ['cat', 'car'], 'b': ['bat', 'bat', 'boy'], 'e' :['大象'],'d':['狗','鹿']}

在“b”键下,“bat”出现两次。如何删除重复项?

我查看了过滤器,lambda,但我不知道如何使用 with(也许 b/c 它是字典中的列表?)

任何帮助,将不胜感激。并提前感谢您的所有帮助!

4

4 回答 4

5

只需在添加之前测试列表中的元素:-

for k in dict.items():
    if k[0] in newdicts:
        if k[1] not in newdicts[k[0]]:  # Do this test before adding.
            newdicts[k[0]].append(k[1])
    else:
        newdicts[k[0]]=[k[1]]

而且由于您只需要列表中的唯一元素value,因此您可以只使用 aSet作为值。此外,您可以在defaultdict此处使用 a,这样您就不必在添加之前测试密钥是否存在。

另外,不要使用内置的变量名。而不是dict其他一些变量。

因此,您可以将merge方法修改为:

from collections import defaultdict

def merge(*d):
    newdicts = defaultdict(set)  # Define a defaultdict
    for each_dict in d:

        # dict.items() returns a list of (k, v) tuple.
        # So, you can directly unpack the tuple in two loop variables.
        for k, v in each_dict.items():  
            newdicts[k].add(v)

    # And if you want the exact representation that you have shown   
    # You can build a normal dict out of your newly built dict.
    unique = {key: list(value) for key, value in newdicts.items()}
    return unique
于 2013-02-08T06:31:54.120 回答
2
>>> import collections
>>> import itertools
>>> uniques = collections.defaultdict(set)
>>> for k, v in itertools.chain(f.items(), g.items(), h.items(), r.items()):
...   uniques[k].add(v)
... 
>>> uniques
defaultdict(<type 'set'>, {'a': set(['apple', 'adam']), 'c': set(['car', 'cat']), 'b':        set(['boy', 'bat']), 'e': set(['elephant']), 'd': set(['deer', 'dog'])})

请注意,结果是在一个集合中,而不是在一个列表中——这种方式的计算效率要高得多。如果您希望最终形式是列表,那么您可以执行以下操作:

>>> {x: list(y) for x, y in uniques.items()}

{'a': ['apple', 'adam'], 'c': ['car', 'cat'], 'b': ['boy', 'bat'], 'e': ['elephant '], 'd': ['鹿', '狗']}

于 2013-02-08T06:33:57.170 回答
1

在你的 for 循环中添加:

for dict in d:
    for k in dict.items():
        if k[0] in newdicts:
            # This line below
            if k[1] not in newdicts[k[0]]:
                newdicts[k[0]].append(k[1])
        else:
            newdicts[k[0]]=[k[1]]

这确保不添加重复项

于 2013-02-08T06:33:45.813 回答
0

当你想要独特的元素时使用 set:

def merge_dicts(*d):
    result={}
    for dict in d:
        for key, value in dict.items():
          result.setdefault(key, set()).add(value)
    return result

尽量避免使用索引;而是解包元组。

于 2013-02-08T07:00:21.907 回答