2024-05-15 06:18:49 发布
网友
在Clojure中,我们有这样一个函数
(reductions str ["foo" "bar" "quax"]) => ["foo" "foobar" "foobarquax"]
或
(reductions + [1 2 3 4 5]) => [1 3 6 10 15]
它基本上只是减少,但它收集中间结果
我在Python中找不到等价物。是否存在基库函数
Python 3
您可以使用^{}
from itertools import accumulate l = [1, 2, 3, 4, 5] print([*accumulate(l)])
印刷品:
[1, 3, 6, 10, 15]
由于Python 3.8,我们可以使用assignment expression :=:
assignment expression :=
data = ["one", "two", "three"] item = "" print([item := item + v for v in data])
您可以使用^{}
印刷品:
由于Python 3.8,我们可以使用
assignment expression :=
:相关问题 更多 >
编程相关推荐