简体   繁体   English

在字典中查找包含相等值的键列表

[英]Finding a list of keys containing equal values in a dictionary

I want to find a list (of lists) of all keys in a dictionary that contain values equal to other elements.我想在字典中找到包含等于其他元素的值的所有键的列表(列表)。

For example:例如:

dict_with_dups = {
    "a": 1,
    "b": 1,
    "c": 1,
    "d": 2,
    "e": 3,
    "f": 3,
    "g": 4,
}

keys_with_same = locate_same_keys(dict_with_dups)

for key_list in keys_with_same:
    print(f"{key_list}")

The above should print this:上面应该打印这个:

['a', 'b', 'c']
['e', 'f']

How do I most efficiently write the function locate_same_keys ?我如何最有效地编写函数locate_same_keys

You can find the duplicate values from the dictionary using a flipped dictionary .您可以使用翻转字典从字典中查找重复值。

You can create it by iterating over the original dictionary and adding each value to the flipped dictionary as a key, and it's key as it's value.您可以通过迭代原始字典并将每个值作为键添加到翻转字典中来创建它,并且它是键即值。 Then, if the value appears again in the original dictionary, add it's key as another value in the flipped dictionary.然后,如果该值再次出现在原始字典中,则将其键添加为翻转字典中的另一个值。

Then you can just go over each key in the flipped dictionary, check if it has more than 1 value and if so, print it:然后你可以查看翻转字典中的每个键,检查它是否有超过 1 个值,如果是,则打印它:

dict_with_dups = {
    "a": 1,
    "b": 1,
    "c": 1,
    "d": 2,
    "e": 3,
    "f": 3,
    "g": 4,
}

# finding duplicate values from dictionary using flip 
flipped = {} 

# iterate over the original dictionary and check if each value is associated 
# with more than one key
for key, value in dict_with_dups.items(): 
    if value not in flipped: 
        flipped[value] = [key] 
    else: 
        flipped[value].append(key) 

# printing all values that are assosiated with more then one key
for key, value in flipped.items():
    if len(value)>1:
        print(value)

Output :输出

['a', 'b', 'c']
['e', 'f']

Regarding efficiency , creating the flipped dictionary requires going over all the key, value pairs in the original dictionary, so we get O(n) time complexity.关于效率,创建翻转字典需要遍历原始字典中的所有键值对,因此我们的时间复杂度为O(n)

Iterate over dict items and for each value add key to proper list.迭代 dict 项目,并为每个值将键添加到正确的列表中。

from collections import defaultdict

res = defaultdict(list)

for k, v in dict_with_dups.items():
    res[v].append(k)

for v in res.values():
    if len(v) > 1:
        print(v)
dict_with_dups = {
    "a": 1,
    "b": 1,
    "c": 1,
    "d": 2,
    "e": 3,
    "f": 3,
    "g": 4,
}
result = {}
for val in dict_with_dups:
    if dict_with_dups[val] in result:
        result[dict_with_dups[val]].append(val)
    else:
        result[dict_with_dups[val]] = [val]    

for key, value in result.items():
   if len(value)>1:
      print(value)

it will give u all list with the same keys like this它将为您提供所有具有相同键的列表

在此处输入图片说明

dict_with_dups = {
    "a": 1,
    "b": 1,
    "c": 1,
    "d": 2,
    "e": 3,
    "f": 3,
    "g": 4,
}

result = list(filter(lambda z: len(z)>1,
                     (list(map(lambda y: y[0],
                               filter(lambda x: x[1]==v, dict_with_dups.items())))
                      for v in set(dict_with_dups.values()))))

or或者

result = [z for z in ([x[0] for x in dict_with_dups.items() if x[1]==v]
                      for v in set(dict_with_dups.values()))
          if len(z)>1]

as suggested by @wjandrea.正如@wjandrea 所建议的那样。

For me, the first variant is more clear (but less concise).对我来说,第一个变体更清晰(但不那么简洁)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM