我正在尝试编写一个在字典中查找重复值的脚本。我的字典有一个整数键和一个列表作为值:
{5: ['13139', '3219', '3'], 6: ['14072', '3214', '3'], 7: ['13137', '3219', '3'], 8: ['13141', '3219', '3'], 9: ['13139', '3219', '3']}
这是我的代码:
for key, value in dict.iteritems():
for other_key, other_value in dict.iteritems():
if value == other_value and key != other_key:
print "We have duplicated values at key {} and key {}".format(key, other_key)
问题是,当我运行脚本时,我得到了重复的行,如下所示:
We have duplicated values at key 5 and key 9
We have duplicated values at key 9 and key 5
因此,我想省略第二行,如果我在两个以上的键上重复了值,那么该脚本也不会显示给我,例如,如果我在键5、9和52上重复了值,它将显示给我: / p>
We have duplicated values at key 5 and key 9
We have duplicated values at key 5 and key 52
We have duplicated values at key 9 and key 5
We have duplicated values at key 9 and key 52
我想告诉我我有duplicated values at key 5, 9 and 52
我还想向我展示所有具有重复值的键,例如,我可以在键5、9和52处进行重复,并在键40和65处进行重复的值。
答案 0 :(得分:2)
您可以将字典从key-> values
转换为新字典value -> keys with associated that value
并以这种方式获得重复项。
示例:
d = {'a':[1,2],'b':[3,1],'c':[2,1,5]}
values_keys = {}
for key in d.keys():
for value in d[key]:
if value not in values_keys:
values_keys[value] = [key]
else:
values_keys[value].append(key)
for key, value in values_keys.items():
if len(value) > 1:
print("key {}: We have duplicated values at keys {}".format(key,','.join(map(str, value))))
答案 1 :(得分:1)
所提供的两种解决方案都不能完全解决问题。要收集重复的值,我们需要创建一个“逆”字典,其关键字是原始字典的值。正如@GeorgeStoyanov指出,键是整数,值是列表,我们需要将这些列表转换为元组,以便能够将它们用作逆字典的键。
from collections import defaultdict
d = {5: ['13139', '3219', '3'], 6: ['14072', '3214', '3'], 7: ['13137', '3219', '3'], 8: ['13141', '3219', '3'],
9: ['13139', '3219', '3']}
val_to_keys = defaultdict(list)
for k, v in d.items():
val_to_keys[tuple(v)].append(k)
for collected_keys in val_to_keys.values():
if len(collected_keys) > 1:
print(collected_keys)
输出:[9, 5]
如果您使用的是Python 2,则可能需要将items()
和values()
更改为iteritems()
和itervalues()
。