[英]Python - comparing values in the same dictionary
我有一本字典:
d = {'Trump': ['MAGA', 'FollowTheMoney'],
'Clinton': ['dems', 'Clinton'],
'Stein': ['FollowTheMoney', 'Atlanta']}
我想删除字符串列表中的重复字符串,这是键的值。
对于此示例,期望的结果是
update_d = {'Trump': ['MAGA'],
'Clinton': ['dems', 'Clinton'],
'Stein': ['Atlanta']}
有一个类似的问题问在这里 ,但我一直没能修改它为我的目的。
我的尝试:
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:
我的问题是我想比较所有键的值并删除重复项。 但是,我不知道该如何实现
您可以使用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()}
不像使用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']}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.