Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在字典中查找包含相等值的键列表_Python_Dictionary - Fatal编程技术网

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

Python 在字典中查找包含相等值的键列表,python,dictionary,Python,Dictionary,我想在字典中找到包含与其他元素相等的值的所有键的列表 例如: 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}") 上面应打

我想在字典中找到包含与其他元素相等的值的所有键的列表

例如:

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}")
上面应打印以下内容:

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

如何最有效地编写函数“查找相同的键”?

迭代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)
它会给你一个所有的列表,上面的键都是一样的


您可以使用翻转字典从字典中找到重复值

您可以通过在原始字典上迭代并将每个值作为键添加到翻转字典中,并将其键作为值添加到翻转字典中来创建它。然后,如果该值再次出现在原始字典中,则将其键添加为翻转字典中的另一个值

然后,您只需检查翻转字典中的每个键,检查其值是否超过1,如果超过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)
输出

['a', 'b', 'c']
['e', 'f']
关于效率,创建翻转字典需要检查原始字典中的所有键、值对,因此我们得到
O(n)
时间复杂度

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()))))

正如@wjandrea所建议的那样


对我来说,第一个变体更清晰(但不那么简洁)。

如果你试图解决它,你就更有可能得到帮助!当您尝试编写该函数(有效或无效)时,出现了什么问题?请尝试阅读有关您的输出的一些问题,这不是要求的。如果。。。否则construction.@GrzegorzBokota我同意。然而,我认为翻转字典解决方案更容易理解。
list(filter(lambda
?使用列表理解代替-它更可读:
[z代表z in…如果len(z)>1]
列表(map(lambda
)使用生成器表达式:
(y[0]代表y in…)
。再想一想,我认为分两步做会更清楚:映射、过滤(不是吹毛求疵,只是大声思考)
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()))))
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]