使用Python matplotlib-venn查找相交值

使用Python matplotlib-venn查找相交值,python,matplotlib,venn-diagram,matplotlib-venn,Python,Matplotlib,Venn Diagram,Matplotlib Venn,我一直在使用matplotlib-venn生成一个包含3组的venn图。如下所示 我想问的是如何找到这些集合的交点的值。例如,与集合A和集合B相交的384个值是什么?与集合A、集合B和集合C相交的144个值是什么?非常值得 多谢各位 罗德里戈 假设您使用的是内置Pythonset对象,获取两个集合之间的交集非常简单。请参见此示例: >>> a = set(range(30)) >>> b = set(range(25,50)) >>> a.i

我一直在使用matplotlib-venn生成一个包含3组的venn图。如下所示

我想问的是如何找到这些集合的交点的值。例如,与集合A和集合B相交的384个值是什么?与集合A、集合B和集合C相交的144个值是什么?非常值得

多谢各位

罗德里戈


假设您使用的是内置Python
set
对象,获取两个集合之间的交集非常简单。请参见此示例:

>>> a = set(range(30))
>>> b = set(range(25,50))
>>> a.intersection(b)
set([25, 26, 27, 28, 29])

我知道这是一个老问题,但我最近研究了一个类似的问题。为了未来的访问者,我在这里分享我的解决方案。下面的代码提供了一个解决方案,用于识别维恩图每个部分的成员资格。仅交点是不够的,还必须减去不需要的集合

def get_venn_sections(sets):
    """
    Given a list of sets, return a new list of sets with all the possible
    mutually exclusive overlapping combinations of those sets.  Another way
    to think of this is the mutually exclusive sections of a venn diagram
    of the sets.  If the original list has N sets, the returned list will
    have (2**N)-1 sets.

    Parameters
    ----------
    sets : list of set

    Returns
    -------
    combinations : list of tuple
        tag : str
            Binary string representing which sets are included / excluded in
            the combination.
        set : set
            The set formed by the overlapping input sets.
    """
    num_combinations = 2 ** len(sets)
    bit_flags = [2 ** n for n in range(len(sets))]
    flags_zip_sets = [z for z in zip(bit_flags, sets)]

    combo_sets = []
    for bits in range(num_combinations - 1, 0, -1):
        include_sets = [s for flag, s in flags_zip_sets if bits & flag]
        exclude_sets = [s for flag, s in flags_zip_sets if not bits & flag]
        combo = set.intersection(*include_sets)
        combo = set.difference(combo, *exclude_sets)
        tag = ''.join([str(int((bits & flag) > 0)) for flag in bit_flags])
        combo_sets.append((tag, combo))
    return combo_sets

@Delgan使用
|
操作符计算并集,而不是交集。在这种情况下,
&
运算符将正确计算交点。matplotlib-venn包很好,但它目前存在一个问题,导致某些集合出现错误。