问题描述
假设我有list_of_numbers = [[1, 2], [3], []]
,我想要简单得多的对象列表对象x = [1, 2, 3]
.
Suppose I have list_of_numbers = [[1, 2], [3], []]
and I want the much simpler object list object x = [1, 2, 3]
.
按照此相关解决方案的逻辑,我愿意
list_of_numbers = [[1, 2], [3], []]
import itertools
chain = itertools.chain(*list_of_numbers)
不幸的是,chain
并不是我想要的,因为(例如)在控制台上运行chain
会返回<itertools.chain object at 0x7fb535e17790>
.
Unfortunately, chain
is not exactly what I want because (for instance) running chain
at the console returns <itertools.chain object at 0x7fb535e17790>
.
函数f
是什么,如果我执行x = f(chain)
然后在控制台上键入x
,我会得到[1, 2, 3]
?
What is the function f
such that if I do x = f(chain)
and then type x
at the console I get [1, 2, 3]
?
更新:实际上,我最终需要的结果是array([1, 2, 3])
.我正在对所选答案的评论中添加一行以解决此问题.
Update: Actually the result I ultimately need is array([1, 2, 3])
. I'm adding a line in a comment on the selected answer to address this.
推荐答案
list
.如果您执行list(chain)
,它将正常工作.但是,仅将其用于调试目的,通常效率低下.
list
. If you do list(chain)
it should work. But use this just for debugging purposes, it could be inefficient in general.
这篇关于从itertools.chain对象获取一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!