【发布时间】:2019-06-26 11:15:30
【问题描述】:
我有一个返回集合的函数(在示例中:some_function())。我得到了一些元素的数据结构(在示例中为arr),需要将元素映射到函数,我想取回一组所有元素。不是一组集合,而是集合中所有元素的集合。我知道some_function() 只返回一维集。
我尝试使用map,但并没有完全发挥作用,我让它与列表推导一起使用,但我不太喜欢我的解决方案。
是否可以不创建列表然后解包?
或者我可以在不做太多工作的情况下以某种方式转换我从map 方法中获得的信息吗?
例子:
arr = [1, 2, 3]
# I want something like this
set.union(some_function(1), some_function(2), some_function(3))
# where some_function returns a set
# this is my current solution
set.union(*[some_function(el) for el in arr]))
# approach with map, but I couldn't convert it back to a set
map(some_function, arr)
【问题讨论】:
标签: python list set list-comprehension set-comprehension