维恩图最多 4 个列表 - 输出交集和唯一集

发布于 2024-08-21 12:26:51 字数 647 浏览 1 评论 0 原文

在我的工作中,我使用了很多维恩图,到目前为止,我一直依赖基于网络的“Venny"。这提供了导出各种交叉点(即仅属于该特定交叉点的元素)的好选项。此外,它还可以绘制最多 4 个列表的图表。

问题是,使用大型列表(4K+ 元素)和超过 3 个集合执行此操作是一件苦差事(复制、粘贴、保存...)。因此,我决定专注于自己生成列表并用它来绘图。

这段冗长的介绍引出了问题的关键。给定 3 或 4 个部分包含相同元素的列表,我如何在 Python 中处理它们以获得各种集合(唯一的、4 个共有的、仅第一个和第二个共有的,等等...),如维恩图所示(< a href="http://www.math.cornell.edu/~numb3rs/lipa/imgs/venn3.png" rel="nofollow noreferrer">3 列表图形示例,4 列表图形示例)?对于 3 个列表来说,看起来并不太难,但是对于 4 个列表来说,就有点复杂了。

in my work I use a lot of Venn diagrams, and so far I've been relying on the web-based "Venny". This offers the nice option to export the various intersections (i.e., the elements belonging only to that specific intersection). Also, it does diagrams up to 4 lists.

Problem is, doing this with large lists (4K+ elements) and more than 3 sets is a chore (copy, paste, save...). Thus, I have decided to focus on generating the lists myself and use it just to plot.

This lengthy introduction leads to the crux of the matter. Given 3 or 4 lists which partially contain identical elements, how can I process them in Python to obtain the various sets (unique, common to 4, common to just first and second, etc...) as shown on the Venn diagram (3 list graphical example, 4 list graphical example)? It doesn't look too hard for 3 lists but for 4 it gets somewhat complex.

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

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

发布评论

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

评论(1

若言繁花未落 2024-08-28 12:26:51

假设你有 python 2.6 或更高版本:

>>> from itertools import combinations
>>>
>>> data = dict(
...   list1 = set(list("alphabet")),
...   list2 = set(list("fiddlesticks")),
...   list3 = set(list("geography")),
...   list4 = set(list("bovinespongiformencephalopathy")),
... )
>>>
>>> variations = {}
>>> for i in range(len(data)):
...   for v in combinations(data.keys(),i+1):
...     vsets = [ data[x] for x in v ]
...     variations[tuple(sorted(v))] = reduce(lambda x,y: x.intersection(y), vsets)
...
>>> for k,v in sorted(variations.items(),key=lambda x: (len(x[0]),x[0])):
...   print "%r\n\t%r" % (k,v)
...
('list1',)
        set(['a', 'b', 'e', 'h', 'l', 'p', 't'])
('list2',)
        set(['c', 'e', 'd', 'f', 'i', 'k', 'l', 's', 't'])
('list3',)
        set(['a', 'e', 'g', 'h', 'o', 'p', 'r', 'y'])
('list4',)
        set(['a', 'c', 'b', 'e', 'g', 'f', 'i', 'h', 'm', 'l', 'o', 'n', 'p', 's', 'r', 't', 'v', 'y'])
('list1', 'list2')
        set(['e', 'l', 't'])
('list1', 'list3')
        set(['a', 'h', 'e', 'p'])
('list1', 'list4')
        set(['a', 'b', 'e', 'h', 'l', 'p', 't'])
('list2', 'list3')
        set(['e'])
('list2', 'list4')
        set(['c', 'e', 'f', 'i', 'l', 's', 't'])
('list3', 'list4')
        set(['a', 'e', 'g', 'h', 'o', 'p', 'r', 'y'])
('list1', 'list2', 'list3')
        set(['e'])
('list1', 'list2', 'list4')
        set(['e', 'l', 't'])
('list1', 'list3', 'list4')
        set(['a', 'h', 'e', 'p'])
('list2', 'list3', 'list4')
        set(['e'])
('list1', 'list2', 'list3', 'list4')
        set(['e'])

Assuming you have python 2.6 or better:

>>> from itertools import combinations
>>>
>>> data = dict(
...   list1 = set(list("alphabet")),
...   list2 = set(list("fiddlesticks")),
...   list3 = set(list("geography")),
...   list4 = set(list("bovinespongiformencephalopathy")),
... )
>>>
>>> variations = {}
>>> for i in range(len(data)):
...   for v in combinations(data.keys(),i+1):
...     vsets = [ data[x] for x in v ]
...     variations[tuple(sorted(v))] = reduce(lambda x,y: x.intersection(y), vsets)
...
>>> for k,v in sorted(variations.items(),key=lambda x: (len(x[0]),x[0])):
...   print "%r\n\t%r" % (k,v)
...
('list1',)
        set(['a', 'b', 'e', 'h', 'l', 'p', 't'])
('list2',)
        set(['c', 'e', 'd', 'f', 'i', 'k', 'l', 's', 't'])
('list3',)
        set(['a', 'e', 'g', 'h', 'o', 'p', 'r', 'y'])
('list4',)
        set(['a', 'c', 'b', 'e', 'g', 'f', 'i', 'h', 'm', 'l', 'o', 'n', 'p', 's', 'r', 't', 'v', 'y'])
('list1', 'list2')
        set(['e', 'l', 't'])
('list1', 'list3')
        set(['a', 'h', 'e', 'p'])
('list1', 'list4')
        set(['a', 'b', 'e', 'h', 'l', 'p', 't'])
('list2', 'list3')
        set(['e'])
('list2', 'list4')
        set(['c', 'e', 'f', 'i', 'l', 's', 't'])
('list3', 'list4')
        set(['a', 'e', 'g', 'h', 'o', 'p', 'r', 'y'])
('list1', 'list2', 'list3')
        set(['e'])
('list1', 'list2', 'list4')
        set(['e', 'l', 't'])
('list1', 'list3', 'list4')
        set(['a', 'h', 'e', 'p'])
('list2', 'list3', 'list4')
        set(['e'])
('list1', 'list2', 'list3', 'list4')
        set(['e'])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文