小编Vod*_*Vod的帖子

使用列表推导来存储最大值

是否可以通过列表理解进行以下操作?试图存储通过循环在任何给定点看到的最大值.

def test(input):
    a = input[0]
    b = []
    for i in input:
        a = max(i,a)
        b.append(a)
    return b

print test([-5,6,19,4,5,20,1,30])

# returns [-5, 6, 19, 19, 19, 20, 20, 30]
Run Code Online (Sandbox Code Playgroud)

python list-comprehension

1
推荐指数
2
解决办法
287
查看次数

标签 统计

list-comprehension ×1

python ×1