从包含重复列表值的字典中获取键,然后将其相应键存储在嵌套列表中

发布于 2025-02-01 11:00:40 字数 839 浏览 1 评论 0 原文

我已经尝试以最好的方式对此进行言论,但是如果我提供了一个试图获得的示例:

输入:

source_dictionary = {"person1": ["x1","x2","x3","x4"],
                     "person2": ["x1","x2","x3","x4"],
                     "person3": ["x1","x2"],
                     "person4": ["x1","x2"],
                     "person5": ["x1","x2"]
                    }

预期输出:

[["person1","person2"],["person3","person4","person5"]]

处理词典中的列表, 可能会更清楚一个挑战。

Appologies,我忘了包括我到目前为止尝试过的东西。如上所述 - 我对列表有问题:

rev_dict = {}
  
for key, value in source_dictionary.items():
    rev_dict.setdefault(value, set()).add(key)
      
result = [key for key, values in rev_dict.items()
                              if len(values) > 1]

I've tried to word this the best way that I possibly can, but it will probably be more clear if I provide an example of what I am trying to acheive:

Input:

source_dictionary = {"person1": ["x1","x2","x3","x4"],
                     "person2": ["x1","x2","x3","x4"],
                     "person3": ["x1","x2"],
                     "person4": ["x1","x2"],
                     "person5": ["x1","x2"]
                    }

Intended output:

[["person1","person2"],["person3","person4","person5"]]

Handling the lists in the dictionary is proving to be quite a challenge.

Appologies, I forgot to include what I have tried so far. As mentioned above - I am having issues with the lists:

rev_dict = {}
  
for key, value in source_dictionary.items():
    rev_dict.setdefault(value, set()).add(key)
      
result = [key for key, values in rev_dict.items()
                              if len(values) > 1]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以笙箫默 2025-02-08 11:00:40

假设您想通过相同的值加入密钥,请使用a

source_dictionary = {"person1": ["x1","x2","x3","x4"],
                     "person2": ["x1","x2","x3","x4"],
                     "person3": ["x1","x2"],
                     "person4": ["x1","x2"],
                     "person5": ["x1","x2"]
                    }

from collections import defaultdict

d = defaultdict(list)
for key, value in source_dictionary.items():
    d[tuple(value)].append(key)
    
out = list(d.values())

setDefault 的替代方案:

d = {}
for key, value in source_dictionary.items():
    d.setdefault(tuple(value), []).append(key)
    
out = list(d.values())

输出:

[['person1', 'person2'], ['person3', 'person4', 'person5']]

Assuming you want to join the keys by identical value, use a defaultdict:

source_dictionary = {"person1": ["x1","x2","x3","x4"],
                     "person2": ["x1","x2","x3","x4"],
                     "person3": ["x1","x2"],
                     "person4": ["x1","x2"],
                     "person5": ["x1","x2"]
                    }

from collections import defaultdict

d = defaultdict(list)
for key, value in source_dictionary.items():
    d[tuple(value)].append(key)
    
out = list(d.values())

Alternative with setdefault:

d = {}
for key, value in source_dictionary.items():
    d.setdefault(tuple(value), []).append(key)
    
out = list(d.values())

output:

[['person1', 'person2'], ['person3', 'person4', 'person5']]
¢好甜 2025-02-08 11:00:40
source_dictionary = {"person1": ["x1","x2","x3","x4"],
                     "person2": ["x1","x2","x3","x4"],
                     "person3": ["x1","x2"],
                     "person4": ["x1","x2"],
                     "person5": ["x1","x2"]
                    }

L = []
for i in source_dictionary.values():
    K = []
    for j in source_dictionary.keys():
        if source_dictionary[j] == i :
            K.append(j)
    if K not in L:
        L.append(K)

print(L)
source_dictionary = {"person1": ["x1","x2","x3","x4"],
                     "person2": ["x1","x2","x3","x4"],
                     "person3": ["x1","x2"],
                     "person4": ["x1","x2"],
                     "person5": ["x1","x2"]
                    }

L = []
for i in source_dictionary.values():
    K = []
    for j in source_dictionary.keys():
        if source_dictionary[j] == i :
            K.append(j)
    if K not in L:
        L.append(K)

print(L)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文