Python 列表理解

发布于 2024-10-14 13:37:51 字数 565 浏览 3 评论 0原文

可能的重复:
运行总计的列表理解

我正在尝试编写一个简洁的列表理解语句来 cdf: 例如:

print f([0.2, 0.3,0.1,0.4])
[0.2,0.5,0.6,1.0] 

标准过程如下所示(我想为函数 f() 编写一个列表理解):

def f(probabilities) :

    sum = 0
    returnList = []
    for count in probabilities:
        sum +=count
        returnList = returnList + [sum]
    return returnList

编辑:我找到了一个函数 numpy.cumsum()。我将检查它是否使用列表理解。

Possible Duplicate:
List comprehension for running total

I'm trying to write a concise list comprehension statement to create a cdf:
For example:

print f([0.2, 0.3,0.1,0.4])
[0.2,0.5,0.6,1.0] 

A standard procedure would look like this (I want to write a list comprehension for the function f()):

def f(probabilities) :

    sum = 0
    returnList = []
    for count in probabilities:
        sum +=count
        returnList = returnList + [sum]
    return returnList

Edit: I found a function numpy.cumsum(). I'll check if it uses list comprehensions.

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

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

发布评论

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

评论(3

—━☆沉默づ 2024-10-21 13:37:51

该操作非常常见,以至于许多语言(主要是函数式语言,但不仅仅是函数式语言)都为其提供了抽象,通常名称为 scanl (它就像具有中间结果的 reduce) 。我们称之为 ireduce(“迭代减少”):

def ireduce(f, state, it):
    for x in it:
        state = f(state, x)
        yield state

现在使用它:

import operator

def f(probabilities):
    return ireduce(operator.add, 0, probabilities)

print(list(f([0.2, 0.3,0.1,0.4])))
# [0.2, 0.5, 0.6, 1.0]

That operation is so common that many languages (mainly functional ones, but not only) provide abstractions for it, usually with the name scanl (it's like a reduce with intermediate results). Let's call it ireduce ("iterative reduce"):

def ireduce(f, state, it):
    for x in it:
        state = f(state, x)
        yield state

And now use it:

import operator

def f(probabilities):
    return ireduce(operator.add, 0, probabilities)

print(list(f([0.2, 0.3,0.1,0.4])))
# [0.2, 0.5, 0.6, 1.0]
木槿暧夏七纪年 2024-10-21 13:37:51
[sum(probabilities[:i+1]) for i in range(len(probabilities))]

但不要这样做,因为它的时间复杂度为 O(n^2)。 Python 列表推导式并不是为此而设计的。使用您已经编写的过程代码。

[sum(probabilities[:i+1]) for i in range(len(probabilities))]

But don't do that because it's O(n^2). Python list comprehensions weren't designed for this. Use the procedural code that you already wrote.

霓裳挽歌倾城醉 2024-10-21 13:37:51

它不是很漂亮,并且没有使用列表推导式,但是您可以使用reduce()函数来做到这一点,其中累积值是一个保存当前总和和结果列表的元组:

a = [0.2, 0.3, 0.1, 0.4]
reduce((lambda result, val: (result[0] + val, result[1] + [result[0] + val])), a, (0, []))[1]

Python缺乏对多行lambda的支持这种丑陋的。使用单独的函数会更好:

    a = [0.2, 0.3, 0.1, 0.4]   
    def accumulate(result, val):
        return (result[0] + val, result[1] + [result[0] + val])

    reduce(accumulate, a, (0, []))[1]

It's not really pretty, and it's not using list comprehensions, but you can do this with the reduce() function, where the accumulated value is a tuple holding the current sum and the result list:

a = [0.2, 0.3, 0.1, 0.4]
reduce((lambda result, val: (result[0] + val, result[1] + [result[0] + val])), a, (0, []))[1]

Python's lack of support for multi-line lambda's makes this kind of ugly. Using a separate function would be better:

    a = [0.2, 0.3, 0.1, 0.4]   
    def accumulate(result, val):
        return (result[0] + val, result[1] + [result[0] + val])

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