使用列表推导将列表的元素附加到另一个列表 [英] Appending a list's elements to another list using a list comprehension
问题描述
我有两个清单:
a = [1,2,3]
b = [4,5,6]
我想要做的是在
a的末尾附加b的所有元素,以便看起来像:
a = [1,2,3,4,5,6]
我可以使用
map(a。追加,b)
如何使用列表推导来做到这一点?
(一般情况下,使用列表理解更好(或者更多
" pythonic")而不是使用地图/过滤器等?)
I have two lists:
a = [1, 2, 3]
b = [4, 5, 6]
What I''d like to do is append all of the elements of b at the end of
a, so that a looks like:
a = [1, 2, 3, 4, 5, 6]
I can do this using
map(a.append, b)
How do I do this using a list comprehension?
(In general, is using a list comprehension preferable (or more
"pythonic") as opposed to using map / filter etc.?)
推荐答案
2007年10月17日星期三20:27:14 +0000,Debajit Adhikary写道:
On Wed, 17 Oct 2007 20:27:14 +0000, Debajit Adhikary wrote:
我有两个清单:
a = [1,2,3]
b = [4,5,6]
我想做的是追加所有的元素b在
a结尾处,所以看起来像:
a = [1,2,3,4,5,6]
我可以用
来做到这一点
map(a.append,b)
I have two lists:
a = [1, 2, 3]
b = [4, 5, 6]
What I''d like to do is append all of the elements of b at the end of
a, so that a looks like:
a = [1, 2, 3, 4, 5, 6]
I can do this using
map(a.append, b)
这是一个坏主意,因为它创建了一个无用的'None` \列表,每个列表一个
元素在`b`。
This is a bad idea as it creates a useless list of `None`\s, one for each
element in `b`.
如何使用列表推导这样做?
How do I do this using a list comprehension?
完全没有。这里显而易见的解决方案是``a.extend(b)``。
Not at all. The obvious solution here is ``a.extend(b)``.
(一般情况下,使用列表理解更好(或更多)
pythonic而不是使用map / filter等?)
(In general, is using a list comprehension preferable (or more
"pythonic") as opposed to using map / filter etc.?)
有人说是。
Ciao,
Marc''BlackJack''Rintsch
Some say yes.
Ciao,
Marc ''BlackJack'' Rintsch
2007年10月17日星期三20:27 +0000,Debajit Adhikary写道:
On Wed, 2007-10-17 at 20:27 +0000, Debajit Adhikary wrote:
我有两个清单:
a = [1,2,3]
b = [4,5,6]
我想做的是在
a结尾附加b的所有元素,所以看起来像:
a = [1,2,3,4,5,6]
我可以用
map(a.append,b)
如何使用列表推导来做到这一点?
I have two lists:
a = [1, 2, 3]
b = [4, 5, 6]
What I''d like to do is append all of the elements of b at the end of
a, so that a looks like:
a = [1, 2, 3, 4, 5, 6]
I can do this using
map(a.append, b)
How do I do this using a list comprehension?
你不是。
You don''t.
(一般情况下,使用列表理解更好(或更多
" pythonic")而不是使用map / filter等?)
(In general, is using a list comprehension preferable (or more
"pythonic") as opposed to using map / filter etc.?)
一般来说,列表理解更像是Pythonic如今,但是在你的特殊情况下你的答案既不是地图也不是列表理解,它是
这个:
a + = b
HTH,
-
Carsten Haese
http://informixdb.sourceforge.net
10月17日晚上9点27分,Debajit Adhikary< debaj ... @ gmail.comwrote:
On Oct 17, 9:27 pm, Debajit Adhikary <debaj...@gmail.comwrote:
我有两个清单:
>
a = [1,2,3]
b = [4,5,6]
我想做的是在
a的末尾附加b的所有元素,以便看起来像e:
a = [1,2,3,4,5,6]
我可以用
map(a.append,b)
如何使用列表推导来做到这一点?
(一般来说,使用列表理解更好(或更多
pythonic)而不是使用map / filter等?)
I have two lists:
a = [1, 2, 3]
b = [4, 5, 6]
What I''d like to do is append all of the elements of b at the end of
a, so that a looks like:
a = [1, 2, 3, 4, 5, 6]
I can do this using
map(a.append, b)
How do I do this using a list comprehension?
(In general, is using a list comprehension preferable (or more
"pythonic") as opposed to using map / filter etc.?)
是的,使用列表理解通常比使用
map / filter更加pythonic。但在这里,正确的答案是:
a.extend(b)。你应该做的第一件事是检查python
库,找到你想要的功能或方法。即使
,你认为你对图书馆了如指掌,仍然值得检查:
我已经失去了我发现的次数一个库
函数完全符合我的要求。
无论如何,如果扩展不存在,那么地图的pythonic版本(a.append,
b)对于b中的x将是
:
a.append(x)
而不是
[a.append(x)for x in b]
列表推导和地图产生一个新列表。这不是你想要的b $ b:你正在使用追加方法的副作用 -
修改了一个。这使得使用常规迭代成为正确的想法,因为
通过使用map或comprehension,你还构建了一个
列表的追加返回值(这是总是没有)。你可以在翻译中看到这个
:
Yes, using a list comprehension is usually more pythonic than using
map/filter. But here, the right answer is:
a.extend(b). The first thing you should always do is check the python
libraries for a function or method that does what you want. Even if
you think you know the library quite well it''s still worth checking:
I''ve lost count of the number of times I''ve discovered a library
function that does exactly what I wanted.
Anyway, if extend didn''t exist, the pythonic version of map(a.append,
b) would be
for x in b:
a.append(x)
Rather than
[a.append(x) for x in b]
List comprehensions and map produce a new list. That''s not what you
want here: you''re using the side-effect of the append method - which
modifies a. This makes using regular iteration the right idea, because
by using map or a comprehension, you''re also constructing a list of
the return values of append (which is always None). You can see this
in the interpreter:
> > map(a.append,b)
>>map(a.append, b)
[无,无,无]
[None, None, None]
>> a
>>a
[1,2,3,4,5,6]
HTH
-
Paul Hankin
[1, 2, 3, 4, 5, 6]
HTH
--
Paul Hankin
这篇关于使用列表推导将列表的元素附加到另一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!