迭代多个集合的交集

Intersection of multiple sets iteratively

我有一个数据框(但它也可以只是 sets/lists):

Group    Letter      
  1    {a,b,c,d,e}
  2    {b,c,d,e,f}
  3    {b,c,d,f,g}
  4    {a,b,c,f,g}
  5    {a,c,d,e,h}

我想添加包含组 1-2、1-2-3、1-2-3-4、1-2-3-4-5 交集的列。 所以它会是这样的:

Group    Letter      Intersection 
  1    {a,b,c,d,e}       None
  2    {b,c,d,e,f}     {b,c,d,e}
  3    {b,c,d,f,g}      {b,c,d}
  4    {a,b,c,f,g}       {b,c}
  5    {a,c,d,e,h}        {c}

我读过 abt np.intersect1d,set.intersection,所以我可以做多个集合的交集。 但我不知道如何以聪明的方式做到这一点。 有人可以帮我解决这个问题吗?

您可能 itertools.accumulate 完成以下任务

import itertools
letters = [{"a","b","c","d","e"},{"b","c","d","e","f"},{"b","c","d","f","g"},{"a","b","c","f","g"},{"a","c","d","e","h"}]
intersections = list(itertools.accumulate(letters, set.intersection))
print(intersections)

产出

[{'e', 'a', 'b', 'c', 'd'}, {'b', 'e', 'c', 'd'}, {'b', 'c', 'd'}, {'b', 'c'}, {'c'}]

请注意第一个元素是 {'e', 'a', 'b', 'c', 'd'} 而不是 None,因此您需要在这方面更改 intersections