Python 运行列表列表中元素的总和

Python 运行列表列表中元素的总和,python,Python,我有一份清单: list =[['x',1,2,3],['y',2,5,4],['z',6,2,1]...] 如何生成运行总和并替换列表中的特定元素,以便: >>>list =[['x',1,2,3],['y',3,7,7],['z',9,9,8]...] 编辑: 好奇为什么不赞成?!更新:我尝试了@Sunitha解决方案,但是 累积不在itertools中-可能是因为运行2.7。我还提出了: 它很笨重,但不管怎样,我是个生物学家。开放给更多的蟒蛇 回答 简单的一行 >

我有一份清单:

list =[['x',1,2,3],['y',2,5,4],['z',6,2,1]...]
如何生成运行总和并替换列表中的特定元素,以便:

>>>list =[['x',1,2,3],['y',3,7,7],['z',9,9,8]...]
编辑:

好奇为什么不赞成?!更新:我尝试了@Sunitha解决方案,但是 累积不在itertools中-可能是因为运行2.7。我还提出了:

它很笨重,但不管怎样,我是个生物学家。开放给更多的蟒蛇 回答


简单的一行

>>> from itertools import accumulate
>>> ll=[['x',1,2,3],['y',2,5,4],['z',6,2,1]]
>>>
>>> list(accumulate(ll, lambda *l: [l[-1][0]] + [sum(r) for r in list(zip(*l))[1:]]))
[['x', 1, 2, 3], ['y', 3, 7, 7], ['z', 9, 9, 8]]
解释

大部分工作由
acculate(ll,func)
code完成,它使用先前计算的结果为iterable
ll
的每个元素运行
func
。我们只需要推动函数
func
就可以完成所需的工作

考虑一下列表的前两个元素

>>> l1=ll[0]; l2=ll[1]
>>> 
>>> l1
['x', 1, 2, 3]
>>> l2
['y', 2, 5, 4]

>>> # A simple zip would create pairs of elements from l1 and l2
>>> f = lambda *l: [list(zip(*l))]
>>> f(l1, l2)
[[('x', 'y'), (1, 2), (2, 5), (3, 4)]]
>>> 
>>> # Remove the first element, so we can calc sum
>>> f = lambda *l: [list(zip(*l))[1:]]
>>> f(l1, l2)
[[(1, 2), (2, 5), (3, 4)]]
>>> 
>>> # Calculate sum for the pairs
>>> f = lambda *l: [sum(r) for r in list(zip(*l))[1:]]
>>> f(l1, l2)
[3, 7, 7]
>>> 
>>> # Now add back the removed first element, but only once
>>> f = lambda *l: [l[-1][0]] + [sum(r) for r in list(zip(*l))[1:]]
>>> f(l1, l2)
['y', 3, 7, 7]
就这样。现在输入此函数进行累加

>>> list(accumulate(ll, f))
[['x', 1, 2, 3], ['y', 3, 7, 7], ['z', 9, 9, 8]]
更新:我尝试了@Sunitha解决方案,但累积不在itertools中-可能是因为运行了2.7

我已经用Python2.7.15和Python3.6.5测试了这段代码。此代码从列表中的第二个子列表(索引1,如果适用)开始,并向后查看前一个子列表以累积值,如示例中所示

Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> hmm = [['x', 1, 2, 3], ['y', 2, 5, 4], ['z', 6, 2, 1]]
>>> for i in range(1, len(hmm)):
...     prev = hmm[i - 1][1:]
...     current = iter(hmm[i])
...     hmm[i] = [next(current)] + [a + b for a, b in zip(prev, current)]
... 
>>> hmm
[['x', 1, 2, 3], ['y', 3, 7, 7], ['z', 9, 9, 8]]
在Python 3中也可以编写稍微不同的代码:

Python 3.6.5 (default, Jun 14 2018, 13:19:33) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> hmm = [['x', 1, 2, 3], ['y', 2, 5, 4], ['z', 6, 2, 1]]
>>> for i in range(1, len(hmm)):
...     _, *prev = hmm[i - 1]
...     letter, *current = hmm[i]
...     hmm[i] = [letter] + [a + b for a, b in zip(prev, current)]
... 
>>> hmm
[['x', 1, 2, 3], ['y', 3, 7, 7], ['z', 9, 9, 8]]

您是否愿意使用numpy或pandas之类的第三方库?如果解决方案更优雅,当然可以。
Python 3.6.5 (default, Jun 14 2018, 13:19:33) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> hmm = [['x', 1, 2, 3], ['y', 2, 5, 4], ['z', 6, 2, 1]]
>>> for i in range(1, len(hmm)):
...     _, *prev = hmm[i - 1]
...     letter, *current = hmm[i]
...     hmm[i] = [letter] + [a + b for a, b in zip(prev, current)]
... 
>>> hmm
[['x', 1, 2, 3], ['y', 3, 7, 7], ['z', 9, 9, 8]]