我怎样才能离开这个结构
>>> input = ['a', 'b', 'c']
这个
>>> output
['a', 'a/b', 'a/b/c']
以一种优雅的(功能性的)方式?
现在我有这个:
>>> from functools import reduce
>>> res = []
>>> for i in range(len(input)):
... res.append(reduce(lambda a, b: a + '/' + b, input[:i+1]))
...
>>> res
['a', 'a/b', 'a/b/c']
最佳答案
您可以使用 itertools.accumulate() :
from itertools import accumulate
l = ['a', 'b', 'c']
print(list(accumulate(l, '{}/{}'.format)))
这个输出:
['a', 'a/b', 'a/b/c']
关于python - 如何从 ('a' , 'a/b' , 'a/b/c' , ('a' ) 得到 'b' , 'c' , 0x104567910 )?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54814893/