[英]Intersection of multiple sets iteratively
I have a dataframe (but it also can be just sets/lists):我有一个 dataframe (但它也可以只是集合/列表):
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}
I want to add column with intersection of group 1-2, 1-2-3, 1-2-3-4, 1-2-3-4-5.我想添加与组 1-2、1-2-3、1-2-3-4、1-2-3-4-5 相交的列。 So it'll be sth like this:
所以它会是这样的:
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}
I've read abt np.intersect1d, set.intersection, so I can do an intersection of multiple sets.我读过 abt np.intersect1d, set.intersection,所以我可以做多个集合的交集。 But I don't know how to do it in smart way.
但我不知道如何以聪明的方式做到这一点。 Can someone help me with this problem?
有人可以帮我解决这个问题吗?
You might itertools.accumulate
for this task as follows你可以为这个任务
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)
output output
[{'e', 'a', 'b', 'c', 'd'}, {'b', 'e', 'c', 'd'}, {'b', 'c', 'd'}, {'b', 'c'}, {'c'}]
Note first element is {'e', 'a', 'b', 'c', 'd'}
rather than None
, so you would need to alter intersections
in that regard.注意第一个元素是
{'e', 'a', 'b', 'c', 'd'}
而不是None
,因此您需要在这方面更改intersections
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.