如何使用列表理解来扩展 python 中的列表?

发布于 2024-11-05 23:24:41 字数 553 浏览 1 评论 0原文

我在 Python 方面没有经验,我经常编写(简化的)如下所示的代码:

accumulationList = []
for x in originalList:
    y = doSomething(x)
    accumulationList.append(y)
return accumulationList

然后在测试通过后,我重构为

return [doSomething(x) for x in originalList]

但假设结果有点不同,我的循环如下所示:

accumulationList = []
for x in originalList:
    y = doSomething(x)
    accumulationList.extend(y)
return accumulationList

其中 doSomething list 返回一个列表。完成这个任务最Pythonic的方法是什么?显然,前面的列表理解会给出一个列表列表。

I'm not experienced in Python, and I often write code that (simplified) looks like this:

accumulationList = []
for x in originalList:
    y = doSomething(x)
    accumulationList.append(y)
return accumulationList

Then after my test passes, I refactor to

return [doSomething(x) for x in originalList]

But suppose it turns out a little different, and my loop looks like this:

accumulationList = []
for x in originalList:
    y = doSomething(x)
    accumulationList.extend(y)
return accumulationList

where the doSomething list returns a list. What is the most Pythonic way to accomplish this? Obviously, the previous list comprehension would give a list of lists.

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

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

发布评论

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

评论(6

伤痕我心 2024-11-12 23:24:41

使用列表理解更简单、更清晰:

[y for x in originalList for y in doSomething(x)]

Much simpler and cleaner with list comprehension:

[y for x in originalList for y in doSomething(x)]
做个少女永远怀春 2024-11-12 23:24:41

你的意思是这样的吗?

accumulationList = []
for x in originalList:
    accumulationList.extend(doSomething(x))
return accumulationList

或更短的代码(但不是最佳的):

return sum((doSomething(x) for x in originalList), [])

或者相同:

return sum(map(doSomething, originalList), [])

感谢@eyquem的提示(如果使用Python 2.x):

import itertools as it

return sum(it.imap(doSomething, originalList), [])

Do you mean something like this?

accumulationList = []
for x in originalList:
    accumulationList.extend(doSomething(x))
return accumulationList

or shorter code (but not optimal):

return sum((doSomething(x) for x in originalList), [])

or the same:

return sum(map(doSomething, originalList), [])

Thanks to @eyquem for the hint (if using Python 2.x):

import itertools as it

return sum(it.imap(doSomething, originalList), [])
傲娇萝莉攻 2024-11-12 23:24:41

我认为涉及 add 或 iadd 的答案在二次时间中运行,这可能不好。我会尝试:

from itertools import chain
accumulation_list = list(chain.from_iterable(doSomething(x) for x in originalList))

I think the answers involving add or iadd run in quadratic time, which probably isn't good. I'd try:

from itertools import chain
accumulation_list = list(chain.from_iterable(doSomething(x) for x in originalList))
南城追梦 2024-11-12 23:24:41

Python 的就地添加运算符(+=,在 operator 模块中以 iadd 形式提供)相当于 .extend对于列表。将其与reduce配对以获得您想要的。

import operator

reduce(operator.iadd, (doSomething(x) for x in originalList)
, accumulation_list)

Python's in-place add operator (+=, available as iadd in operator module) is equivalent of .extend for list. Pair it with reduce to get what you want.

import operator

reduce(operator.iadd, (doSomething(x) for x in originalList)
, accumulation_list)
自在安然 2024-11-12 23:24:41

我认为这种情况没有特殊的语法。但是你可以使 for 循环更短:

accumulationList += doSomething(x)

如果你坚持,你可以使用函数式编程来展平列表:

result = reduce(lambda a,b: a+b, [[i,i*2] for i in range(3)])

但我不会称其为 pythonic,我认为它比 for 循环更难阅读。

I don't think there is special syntax for this case. But you could make the for loop shorter:

accumulationList += doSomething(x)

If you insist, you could use functional programming to flatten the list:

result = reduce(lambda a,b: a+b, [[i,i*2] for i in range(3)])

But I wouldn't call this pythonic, I think it's harder to read than a for loop.

卷耳 2024-11-12 23:24:41

功能上,您可以使用itertools.chain地图。对于输入列表 L

res = list(chain.from_iterable(map(doSomething, L)))

如果您需要迭代器,只需删除 list 调用即可。这是一个演示:

def doSomething(value):
    return [value * i for i in range(1, 4)]

def original(originalList):
    accumulationList = []
    for x in originalList:
        y = doSomething(x)
        accumulationList.extend(y)
    return accumulationList

def new(L):
    return list(chain.from_iterable(map(doSomething, L)))

x = [1, 2, 3]

assert original(x) == new(x)

print(new(x))

[1, 2, 3, 2, 4, 6, 3, 6, 9]

Functionally, you can use itertools.chain with map. For an input list L:

res = list(chain.from_iterable(map(doSomething, L)))

If you need an iterator, simply remove the list call. Here's a demo:

def doSomething(value):
    return [value * i for i in range(1, 4)]

def original(originalList):
    accumulationList = []
    for x in originalList:
        y = doSomething(x)
        accumulationList.extend(y)
    return accumulationList

def new(L):
    return list(chain.from_iterable(map(doSomething, L)))

x = [1, 2, 3]

assert original(x) == new(x)

print(new(x))

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