[英]Finding the intersection between several sets in a list
I have a list containing several sets {} with different names and string values.我有一个列表,其中包含多个具有不同名称和字符串值的集合 {}。
eg.例如。
set1 = {"a", "santa", "clock"}
search = {"why", "is", "santa", "nice"}
list_of_sets = [set1, search]
I want to find the intersection between the sets in the list through a function:我想通过一个函数找到列表中集合之间的交集:
def intersection_between_sets_from_list(list_of_sets):
""" find the intersection between the sets here"""
return intersection_strings
any idea what code to use in """ find the intersection between the sets here""".任何想法在“”中使用什么代码在这里找到集合之间的交集“””。
If you want find the intersection between all sets in the list you could be iterate on list_of_set
and find the intersection between the current set and your temporal set like this:如果你想找到列表中所有集合之间的交集,你可以在
list_of_set
上迭代并找到当前集和你的时间集之间的交集,如下所示:
tmp = list_of_sets[0]
for e in list_of_sets:
tmp = tmp.intersection(e)
but this operation on python language could be do:但是这个python语言的操作可以做:
reduce(lambda x, y: x.intersection(y), list_of_sets)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.