gpt4 book ai didi

python - 在 python 中的三个重叠集中查找增量

转载 作者:太空宇宙 更新时间:2023-11-04 09:43:12 25 4
gpt4 key购买 nike

x = [1,2,3,4,5,6,7]
y = [2,1,4,5,8,10]
z = [7,1,3,6,0]

如何在这三个列表的维恩图中找到所有元素(或者我什至可以使用集合)

enter image description here

我正在使用大小为 500-1000 的 DataFrame。我尝试使用数据帧的条件子集。在 python 中有更简单、更简单的方法吗?我对集合、numpy 数组甚至列表持开放态度。

最佳答案

给定:

x = [1,2,3,4,5,6,7]
y = [2,1,4,5,8,10]
z = [7,1,3,6,0]

尝试:

x = set([1,2,3,4,5,6,7])
y = set([2,1,4,5,8,10])
z = set([7,1,3,6,0])

然后您可以通过以下方式找到交叉点:

xy = x.intersection(y)
xyz = x.intersection(y).intersection(z)
# and
xyz = x & y & z

另请参阅:https://docs.python.org/2/library/sets.html#set-objects

所有增量:

我使用约定“x_”表示“不在 x 中”

x_y_z = z.difference(x).difference(y)
x_yz_ = y.difference(x).difference(z)
x_yz = y.intersection(z).difference(x)
xy_z_ = x.difference(y).difference(z)
xy_z = x.difference(y).intersection(z)
xyz_ = x.intersection(y).difference(z)
xyz = x.intersection(y).intersection(z)

关于python - 在 python 中的三个重叠集中查找增量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50897552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com