Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass list of sets as separate arguments into function? [duplicate]

I've created a list of sets that I'd like to pass into set.intersection()

For example:

List_of_Sets = [{1,2,3},{3,4,5},{5,6,7}]
set.intersection(List_of_Sets)

Result:

TypeError: descriptor 'intersection' requires a 'set' object but received a 'list'

Desired Output:

{3,5}

How would I pass each set within the list as a separate argument into set.intersection()?

like image 226
Chris Avatar asked Feb 15 '23 00:02

Chris


1 Answers

Use the unpacking operator: set.intersection(*List_of_Sets)


As pointed out in the other answer, you have no intersections in the list. Do you want to compute the union of the intersection of adjacent elements?

>>> set.union(*[x & y for x, y in zip(List_of_Sets, List_of_Sets[1:])])
set([3, 5])
like image 117
mgilson Avatar answered Mar 23 '23 00:03

mgilson