列表理解问题

发布于 2024-10-04 20:06:59 字数 209 浏览 2 评论 0原文

有没有办法在每次迭代的列表理解中将多个项目添加到列表中?例如:

y = ['a', 'b', 'c', 'd']
x = [1,2,3]

return [x, a for a in y]

输出:[[1,2,3], 'a', [1,2,3], 'b', [1,2,3], 'c', [1,2, 3], 'd']

Is there a way to add multiple items to a list in a list comprehension per iteration? For example:

y = ['a', 'b', 'c', 'd']
x = [1,2,3]

return [x, a for a in y]

output: [[1,2,3], 'a', [1,2,3], 'b', [1,2,3], 'c', [1,2,3], 'd']

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

-残月青衣踏尘吟 2024-10-11 20:06:59

当然有,但不是简单的列表理解:

编辑:受到另一个答案的启发:

y = ['a', 'b', 'c', 'd']
x = [1,2,3]

return sum([[x, a] for a in y],[])

它是如何工作的:sum会添加一系列的东西,只要有一个__add__成员来完成这项工作。但是,它的初始总数为 0。您无法将 0 添加到列表中,但可以为 sum() 提供另一个起始值。这里我们使用一个空列表。

如果您不需要一个实际的列表,而只需要一个生成器,则可以使用itertools.chain.from_iterable,它只是将一堆迭代器串成一个长迭代器。

from itertools import *

return chain.from_iterable((x,a) for a in y)

或者对 itertools 更友好:

return itertools.chain.from_iterable(itertools.izip(itertools.repeat(x),y))

当然,还有其他方法:首先,我们可以改进 Adam Rosenfield 的 通过消除不需要的 lambda 表达式来回答

return reduce(list.__add__,([x, a] for a in y))

因为 list 已经有一个成员可以完全满足我们的需要。我们可以使用 maplist.extend 中的副作用来实现相同的效果:

l = []
map(l.extend,[[x, a] for a in y])
return l

最后,让我们尝试尽可能不优雅的纯列表理解:

return [ y[i/2] if i%2 else x for i in range(len(y)*2)]

sure there is, but not with a plain list comprehension:

EDIT: Inspired by another answer:

y = ['a', 'b', 'c', 'd']
x = [1,2,3]

return sum([[x, a] for a in y],[])

How it works: sum will add a sequence of anythings, so long as there is a __add__ member to do the work. BUT, it starts of with an initial total of 0. You can't add 0 to a list, but you can give sum() another starting value. Here we use an empty list.

If, instead of needing an actual list, you wanted just a generator, you can use itertools.chain.from_iterable, which just strings a bunch of iterators into one long iterator.

from itertools import *

return chain.from_iterable((x,a) for a in y)

or an even more itertools friendly:

return itertools.chain.from_iterable(itertools.izip(itertools.repeat(x),y))

There are other ways, too, of course: To start with, we can improve Adam Rosenfield's answer by eliminating an unneeded lambda expression:

return reduce(list.__add__,([x, a] for a in y))

since list already has a member that does exactly what we need. We could achieve the same using map and side effects in list.extend:

l = []
map(l.extend,[[x, a] for a in y])
return l

Finally, lets go for a pure list comprehension that is as inelegant as possible:

return [ y[i/2] if i%2 else x for i in range(len(y)*2)]
夏日落 2024-10-11 20:06:59

这是一种方法:

y = ['a', 'b', 'c', 'd']
x = [1,2,3]

return reduce(lambda a,b:a+b, [[x,a] for a in y])

Here's one way:

y = ['a', 'b', 'c', 'd']
x = [1,2,3]

return reduce(lambda a,b:a+b, [[x,a] for a in y])
冷清清 2024-10-11 20:06:59
x = [1,2,3]
y = ['a', 'b', 'c', 'd']
z = []

[z.extend([x, a]) for a in y]

(正确的值将在 z 中)

x = [1,2,3]
y = ['a', 'b', 'c', 'd']
z = []

[z.extend([x, a]) for a in y]

(The correct value will be in z)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文