叮当猫咪
更新:我尝试了@Sunitha解决方案,但在itertools中没有积累-可能是因为运行2.7。我已经使用Python 2.7.15和Python 3.6.5测试了此代码。此代码从列表中的第二个子列表(索引1,如果适用)开始,并向后看前一个子列表,以累积值,如您的示例一样。Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) [GCC 7.3.0] on linux2Type "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 linuxType "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]]