在我的工作中,我使用了很多维恩图,到目前为止,我一直依赖基于网络的“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.
发布评论
评论(1)
假设你有 python 2.6 或更高版本:
Assuming you have python 2.6 or better: