Python等价于clojure约化

2024-05-15 06:18:49 发布

您现在位置:Python中文网/ 问答频道 /正文

在Clojure中,我们有这样一个函数

(reductions str ["foo" "bar" "quax"])
=> ["foo" "foobar" "foobarquax"]

(reductions + [1 2 3 4 5])
=> [1 3 6 10 15]

它基本上只是减少,但它收集中间结果

我在Python中找不到等价物。是否存在基库函数

Python 3


Tags: 函数foobarfoobarclojurestr库函数foobarquax
2条回答

您可以使用^{}

from itertools import accumulate

l = [1, 2, 3, 4, 5]

print([*accumulate(l)])

印刷品:

[1, 3, 6, 10, 15]

由于Python 3.8,我们可以使用assignment expression :=

data = ["one", "two", "three"]

item = ""
print([item := item + v for v in data])

相关问题 更多 >

    热门问题