Python - 比较同一个字典中的值

时间:2016-12-05 23:29:52

标签: python

我有一本字典:

d = {'Trump': ['MAGA', 'FollowTheMoney'],
     'Clinton': ['dems', 'Clinton'],
     'Stein': ['FollowTheMoney', 'Atlanta']}

我想删除字符串列表中的重复字符串,这是字符串的值。

对于此示例,所需的结果是

update_d = {'Trump': ['MAGA'],
            'Clinton': ['dems', 'Clinton'],
            'Stein': ['Atlanta']}

有一个类似的问题here,但我无法为我的目的修改它。

我的尝试:

new_d = {}
for key in d:  
    for key2 in d:
        lst = d[key]
        lst2 = d[key2]

        for string in lst:  
           for string2 in lst2:
              if string not in new_d:

我的问题是我要比较所有键的值并删除重复项。但是,我不知道如何实现这一目标

2 个答案:

答案 0 :(得分:1)

您可以使用Counter来计算每个值在d中出现的次数。

d = {'Trump': ['MAGA', 'FollowTheMoney'],
     'Clinton': ['dems', 'Clinton'],
     'Stein': ['FollowTheMoney', 'Atlanta']}

from collections import Counter

c = Counter(x for xs in d.values() for x in xs)

在此示例中,c的值为

Counter({'Atlanta': 1,
         'Clinton': 1,
         'FollowTheMoney': 2,
         'MAGA': 1,
         'dems': 1})

然后选择计数恰好为1的值。

update_d = {k: [v for v in vs if c[v] == 1] for k, vs in d.items()}

答案 1 :(得分:0)

不如使用Counter那么优雅,但在不使用模块的情况下删除重复项:

d = {'Trump': ['MAGA', 'FollowTheMoney'],
    'Clinton': ['dems', 'Clinton'],
    'Stein': ['FollowTheMoney', 'Atlanta']}

dupvals = [item for sublist in d.values() for item in sublist] # get all values from all keys into a list
dups = [] # list to hold duplicates

for i in dupvals:
    if dupvals.count(i) > 1:
        dups.append(i)

dupvals = set(dups) # keep only one item for each duplicated item

new_d = {}

for key,values in d.items():
    for value in values:
        if not value in dupvals:
            new_d.setdefault(key, []).append(value)

print new_d # {'Clinton': ['dems', 'Clinton'], 'Trump': ['MAGA'], 'Stein': ['Atlanta']}