python 字典项的交集

发布于 2024-12-09 23:53:37 字数 232 浏览 4 评论 0原文

假设我有一个像这样的字典:

aDict[1] = '3,4,5,6,7,8'
aDict[5] = '5,6,7,8,9,10,11,12'
aDict[n] = '5,6,77,88'

键是任意的,并且可以有任意数量。我想考虑字典中的每个值。

我想将每个字符串视为逗号分隔值,并找到整个字典的交集(所有字典值共有的元素)。所以在这种情况下答案将是“5,6”。我该怎么做?

Suppose I have a dict like:

aDict[1] = '3,4,5,6,7,8'
aDict[5] = '5,6,7,8,9,10,11,12'
aDict[n] = '5,6,77,88'

The keys are arbitrary, and there could be any number of them. I want to consider every value in the dictionary.

I want to treat each string as comma-separated values, and find the intersection across the entire dictionary (the elements common to all dict values). So in this case the answer would be '5,6'. How can I do this?

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

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

发布评论

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

评论(5

猫瑾少女 2024-12-16 23:53:37
from functools import reduce # if Python 3

reduce(lambda x, y: x.intersection(y), (set(x.split(',')) for x in aDict.values()))
from functools import reduce # if Python 3

reduce(lambda x, y: x.intersection(y), (set(x.split(',')) for x in aDict.values()))
婴鹅 2024-12-16 23:53:37

首先,您需要将它们转换为真实的列表。

l1 = '3,4,5,6,7,8'.split(',')

然后就可以用集合来做交集了。

result = set(l1) & set(l2) & set(l3)

First of all, you need to convert these to real lists.

l1 = '3,4,5,6,7,8'.split(',')

Then you can use sets to do the intersection.

result = set(l1) & set(l2) & set(l3)
极致的悲 2024-12-16 23:53:37

Python Set 非常适合该任务。考虑以下内容(代码):

intersections = None
for value in aDict.values():
    temp = set([int(num) for num in value.split(",")])
    if intersections is None:
        intersections = temp
    else:
        intersections = intersections.intersection(temp)

print intersections

Python Sets are ideal for that task. Consider the following (pseudo code):

intersections = None
for value in aDict.values():
    temp = set([int(num) for num in value.split(",")])
    if intersections is None:
        intersections = temp
    else:
        intersections = intersections.intersection(temp)

print intersections
爱人如己 2024-12-16 23:53:37
result = None
for csv_list in aDict.values():
    aList = csv_list.split(',')
    if result is None:
        result = set(aList)
    else:
        result = result & set(aList)
print result
result = None
for csv_list in aDict.values():
    aList = csv_list.split(',')
    if result is None:
        result = set(aList)
    else:
        result = result & set(aList)
print result
亢潮 2024-12-16 23:53:37

由于 set.intersection() 接受任意数量的集合,因此您可以在不使用 reduce() 的情况下凑合:

set.intersection(*(set(v.split(",")) for v in aDict.values()))

请注意,此版本不适用于空 <代码>aDict。

如果您使用的是 Python 3,并且您的字典值是 bytes 对象而不是字符串,只需在 b"," 处分割,而不是 "," >。

Since set.intersection() accepts any number of sets, you can make do without any use of reduce():

set.intersection(*(set(v.split(",")) for v in aDict.values()))

Note that this version won't work for an empty aDict.

If you are using Python 3, and your dictionary values are bytes objects rather than strings, just split at b"," instead of ",".

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