如何使用生成器表达式创建多个集合的并集? [英] How to create the union of many sets using a generator expression?

查看:88
本文介绍了如何使用生成器表达式创建多个集合的并集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个集合列表,并且我想获得该列表中所有集合的并集.有什么办法可以使用生成器表达式来做到这一点吗?换句话说,我如何直接在列表 中以frozenset的形式在所有集合上创建并集?

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?

推荐答案

只需使用 .union()方法.

Just use the .union() method.

>>> l = [set([1,2,3]), set([4,5,6]), set([1,4,9])]
>>> frozenset().union(*l)
frozenset([1, 2, 3, 4, 5, 6, 9])

这适用于任何可迭代的项目.

This works for any iterable of iterables.

这篇关于如何使用生成器表达式创建多个集合的并集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆