Suppose I have a list of sets and I want to get the union over all sets in that list. Is there any way to do this using a generator expression? In other words, how can I create the union over all sets in that list directly as a frozenset
?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Nested generator expression. But I think they are a bit cryptic, so the way KennyTM suggested may be clearer.
I assume that what you're trying to avoid is the intermediate creations of frozenset objects as you're building up the union?
Here's one way to do it. NOTE: this originally used
itertools.chain()
but, as Kenny's comment notes, the version below is slightly better:Invoke like this:
Just use the
.union()
method.This works for any iterable of iterables.