下面的问题是关于Python3.6的。假设我有集合列表,例如

L1 = [{2,7},{2,7,8},{2,3,6,7},{1,2,4,5,7}]
L2 = [{3,6},{1,3,4,6,7},{2,3,5,6,8}]
L3 = [{2,5,7,8},{1,2,3,5,7,8}, {2,4,5,6,7,8}]

我需要找到L1、L2和L3的每个元素之间的所有交集。例如。:
    {2,7}.intersection({3,6}).intersection({2,5,7,8})= empty
    {2,7}.intersection({3,6}).intersection({1,2,3,5,7,8})= empty
    {2,7}.intersection({3,6}).intersection({2,4,5,6,7,8})= empty
    {2,7}.intersection({1,3,4,6,7}).intersection({2,5,7,8})= {7}
    {2,7}.intersection({1,3,4,6,7}).intersection({1,2,3,5,7,8})= {7}
    {2,7}.intersection({1,3,4,6,7}).intersection({2,4,5,6,7,8})= {7}

...............................
如果我们继续这样做,我们最终会得到以下结果:
{{empty},{2},{3},{6},{7},{2,3},{2,5},{2,6},{2,8},{3,7},{4,7},{6,7}
假设:
-我有很多单子L1,L2,L3,…Ln。我也不知道我有多少单子。
-每个列表L1,L2,L3..Ln都很大,所以我无法将它们全部加载到内存中。
我的问题是:有没有任何方法可以按顺序计算这个集合,例如,在L1和L2之间计算,然后用结果计算L3,等等。。。

最佳答案

首先可以计算L1和L2之间的所有可能交点,然后计算该集和L3之间的交点,依此类推。

list_generator = iter([  # some generator that produces your lists
    [{2,7}, {2,7,8}, {2,3,6,7}, {1,2,4,5,7}],
    [{3,6}, {1,3,4,6,7}, {2,3,5,6,8}],
    [{2,5,7,8}, {1,2,3,5,7,8}, {2,4,5,6,7,8}],
])
# for example, you can read from a file:
# (adapt the format to your needs)
def list_generator_from_file(filename):
    with open(filename) as f:
        for line in f:
            yield list(map(lambda x: set(x.split(',')), line.strip().split('|')))
# list_generator would be then list_generator_from_file('myfile.dat')

intersections = next(list_generator)  # get first list
new_intersections = set()

for list_ in list_generator:
    for old in intersections:
        for new in list_:
            new_intersections.add(frozenset(old.intersection(new)))
    # at this point we don't need the current list any more
    intersections, new_intersections = new_intersections, set()

print(intersections)

输出看起来像{frozenset({7}), frozenset({3, 7}), frozenset({3}), frozenset({6}), frozenset({2, 6}), frozenset({6, 7}), frozenset(), frozenset({8, 2}), frozenset({2, 3}), frozenset({1, 7}), frozenset({4, 7}), frozenset({2, 5}), frozenset({2})},它与您所拥有的匹配,除了您错过的{1,7}集合。

关于python - 查找集合列表之间的交集,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49206395/

10-12 20:08