Mapping set operations in python
Question:
I have a very simple problem, but today, I confess I have trouble finding the solution:
I want to use the ‘&’ operator on python sets, but I don’t know how many sets I will have to deal with.
Is it possible to use a mapping with python to achieve this?
Thanks !
Answers:
You can simply use
set.intersection(*some_list_of_sets)
If you still want to use the operator,
functools.reduce(operator.and_, some_list_of_sets)
I have a very simple problem, but today, I confess I have trouble finding the solution:
I want to use the ‘&’ operator on python sets, but I don’t know how many sets I will have to deal with.
Is it possible to use a mapping with python to achieve this?
Thanks !
You can simply use
set.intersection(*some_list_of_sets)
If you still want to use the operator,
functools.reduce(operator.and_, some_list_of_sets)