在列表理解中生成多个项目 [英] Producing multiple items in a list comprehension
问题描述
是否有一种简单的方法可以获得列表理解,为每个输入参数生成一个平面列表,
,[x,2 * x]?
例如,我想做类似的事情:
[[x,2 * x] x范围(4)]
....并收到
[0,0,1,2,2,4,3,6]
....但是你当然得到一份清单清单:
[[0,0],[1,2],[2,4],[ 3,6]]
我知道我可以使用任何标准的扁平一些代码将这个
重新变成我想要的东西,但我希望有一些方法可以避免清单列出
列表。第一个世代?
一个稍微类似的问题:如果我想合并,比如说,list1 = [1,2,3]用
list2 = [4,5,6]获得[1,4,2,5,3,6],是否有一些巧妙的方法带有zip的
这样做?
谢谢,
--- Joel
Joel Koltner写道:
有没有一种简单的方法可以获得列表理解来生成一个平面列表,
每个输入参数
说,[x,2 * x]?
例如,我想做类似的事情:
[[x,2 * x] for x in range(4)]
...并且收到
[ 0,0,1,2,2,4,3,6]
...但当然你真的得到了一份清单清单:
[[0,0],[1,2],[2,4],[3,6]]
我知道我可以使用任何标准的扁平一些代码将这个
重新变成我想要的东西,但我希望有一些方法可以避免清单列出
列表。第一个世代?
一个稍微类似的问题:如果我想合并,比如说,list1 = [1,2,3]用
list2 = [4,5,6]获得[1,4,2,5,3,6],是否有一些巧妙的方法带有zip的
这样做?
谢谢,
--- Joel
-
http://mail.python.org/ mailman / listinfo / python-list
第一部分:
def gen(n):
我在xrange(n)的
:
收益率我
收益率2 * i
打印列表(gen(4))
[0,0,1,2,2,4,3,6]
gerard
Joel Koltner写道:
有一种简单的方法可以获得列表理解来生成一个平面列表
每个输入参数的
,比如,[x,2 * x]?
例如,我想做类似的事情:
[[x,2 * x] for x in range(4)]
...并且收到
>
[0,0,1,2,2,4,3,6]
...但当然你真的得到了一份清单:
[[0,0],[1,2], [2,4],[3,6]]
我知道我可以使用任何标准的扁平将这些代码转回我想要的代码,但我希望有一些方法可以避免
列表清单。一代人?
>> [x * y表示范围内的x(4 )for 1,2 in
[0,0,1,2,2,4,3,6]
一个稍微类似的问题:如果我想要合并,比如说,list1 = [1,2,3]用
list2 = [4,5,6]获得[1,4,2,5,3,6],是否有一些巧妙的方法用zip
这样做?
>> items = [None] * 6 > items [:: 2] = 1,2,3
items [1 :: 2] = 4,5,6
items
[1,4,2,5,3,6]
Peter
Joel Koltner写道:
是否有一种简单的方法可以获得列表理解来生成一个平面列表,
,[x,每个输入参数2 * x]?
例如,我想做类似的事情:
[[x,2] * x] for x in range(4)]
...并且收到
[0,0,1,2,2 ,4,3,6]
...但当然你真的得到一份清单清单:
[[0,0} ],[1,2],[2,4],[3,6]]
我知道我可以使用任何标准的展平。一些代码将这个
重新变成我想要的东西,但我希望有一些方法可以避免清单列出
列表。第一个世代?
一个稍微类似的问题:如果我想合并,比如说,list1 = [1,2,3]用
list2 = [4,5,6]获得[1,4,2,5,3,6],是否有一些巧妙的方法带有zip的
这样做?
谢谢,
--- Joel
-
http://mail.python.org/ mailman / listinfo / python-list
第一部分:
def gen(n):
我在xrange(n)的
:
收益率我
收益率2 * i
打印列表(gen(4))
[0,0,1,2,2,4,3,6]
gerard
Is there an easy way to get a list comprehension to produce a flat list of,
say, [x,2*x] for each input argument?
E.g., I''d like to do something like:
[ [x,2*x] for x in range(4) ]
....and receive
[ 0,0,1,2,2,4,3,6]
....but of course you really get a list of lists:
[[0, 0], [1, 2], [2, 4], [3, 6]]
I''m aware I can use any of the standard "flatten" bits of code to turn this
back into what I want, but I was hoping there''s some way to avoid the "lists
of lists" generation in the first place?
A slightly similar problem: If I want to "merge," say, list1=[1,2,3] with
list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way with "zip" to
do so?
Thanks,
---Joel
Joel Koltner wrote:Is there an easy way to get a list comprehension to produce a flat list of,
say, [x,2*x] for each input argument?
E.g., I''d like to do something like:
[ [x,2*x] for x in range(4) ]
...and receive
[ 0,0,1,2,2,4,3,6]
...but of course you really get a list of lists:
[[0, 0], [1, 2], [2, 4], [3, 6]]
I''m aware I can use any of the standard "flatten" bits of code to turn this
back into what I want, but I was hoping there''s some way to avoid the "lists
of lists" generation in the first place?
A slightly similar problem: If I want to "merge," say, list1=[1,2,3] with
list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way with "zip" to
do so?
Thanks,
---Joel
--
http://mail.python.org/mailman/listinfo/python-list
For the first part:
def gen(n):
for i in xrange(n):
yield i
yield 2*i
print list(gen(4))
[0, 0, 1, 2, 2, 4, 3, 6]
gerard
Joel Koltner wrote:
Is there an easy way to get a list comprehension to produce a flat list
of, say, [x,2*x] for each input argument?
E.g., I''d like to do something like:
[ [x,2*x] for x in range(4) ]
...and receive
[ 0,0,1,2,2,4,3,6]
...but of course you really get a list of lists:
[[0, 0], [1, 2], [2, 4], [3, 6]]
I''m aware I can use any of the standard "flatten" bits of code to turn
this back into what I want, but I was hoping there''s some way to avoid the
"lists of lists" generation in the first place?
>>[x*y for x in range(4) for y in 1,2]
[0, 0, 1, 2, 2, 4, 3, 6]
A slightly similar problem: If I want to "merge," say, list1=[1,2,3] with
list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way with "zip"
to do so?
>>items = [None] * 6
items[::2] = 1,2,3
items[1::2] = 4,5,6
items
[1, 4, 2, 5, 3, 6]
Peter
Joel Koltner wrote:Is there an easy way to get a list comprehension to produce a flat list of,
say, [x,2*x] for each input argument?
E.g., I''d like to do something like:
[ [x,2*x] for x in range(4) ]
...and receive
[ 0,0,1,2,2,4,3,6]
...but of course you really get a list of lists:
[[0, 0], [1, 2], [2, 4], [3, 6]]
I''m aware I can use any of the standard "flatten" bits of code to turn this
back into what I want, but I was hoping there''s some way to avoid the "lists
of lists" generation in the first place?
A slightly similar problem: If I want to "merge," say, list1=[1,2,3] with
list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way with "zip" to
do so?
Thanks,
---Joel
--
http://mail.python.org/mailman/listinfo/python-list
For the first part:
def gen(n):
for i in xrange(n):
yield i
yield 2*i
print list(gen(4))
[0, 0, 1, 2, 2, 4, 3, 6]
gerard
这篇关于在列表理解中生成多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!