简体   繁体   English

对列表中所有元素求和,除了第一个

[英]sum all the elements in the list of lists except first

Add all the elements in the list of lists except the first element and make a new list. 添加列表列表中除第一个元素以外的所有元素,并创建一个新列表。

  l = [[u'Security', -604.5, -604.5, -604.5, 
       -302.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -2115.75], 
       [u'Medicare', -141.38, -141.38, -141.38, -70.69, 
       0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -494.83], 
       [u'Insurance', -338.0, -338.0, -338.0, -169.0, 0.0, 
       0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1183.0]]

Output should look like 输出应该看起来像

['total',-1083.88,-1083.88,-1083.88,-541.94,0.0,0.0,0.0,0.0,0.0,0.0,
   0.0,0.0,-3793.58]

Eg: -1083.88 of the output list is = -604.5+(-141.38)+(-338.0)=-1083.88 例如:输出列表的-1083.88 = -604.5 +(-141.38)+(-338.0)=-1083.88

I've tried like this 我已经试过了

for i in r:
   del(i[0])
total = [sum(i) for i in zip(*r)]

Going by your expected output, I believe you're looking for a transposition and sum on the columns. 按照您的预期输出,我相信您正在寻找列的转置和求和。 You can use zip for that. 您可以为此使用zip

r = [sum(x) if not isinstance(x[0], str) else 'total' for x in zip(*l)]

print(r)
['total', -1083.88, -1083.88, -1083.88, -541.94, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -3793.58]

Alternatively, convert the transposal to a list and you can avoid the if check (this is similar to MaximTitarenko's answer , so credit to them as well). 或者,将转座转换为list ,您可以避免if检查(这类似于MaximTitarenko的answer ,因此也应归功于它们)。

r = [sum(x) for x in list(zip(*l))[1:]]
r.insert(0, 'total')

Or, if you'd prefer, 或者,如果您愿意,

r = ['total'] + [sum(x) for x in list(zip(*l))[1:]]

Which is a little less elegant. 优雅一点。

You can try this: 您可以尝试以下方法:

result = ['total'] + [sum(el) for el in list(zip(*l))[1:]] 

print(result)
# ['total', -1083.88, -1083.88, -1083.88, -541.94, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -3793.58]

To use all python flavour you need to use itertools.islice since in Python zip() returns iterator and you cannot just [1:] subscript zip object. 要使用所有python itertools.islice您需要使用itertools.islice因为在python中, zip()返回迭代器,而您不能仅使用[1:]下标zip对象。

In [1]: l = [[u'Security', -604.5, -604.5, -604.5,
   ...:    ...:        -302.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -2115.75
   ...: ],
   ...:    ...:        [u'Medicare', -141.38, -141.38, -141.38, -70.69,
   ...:    ...:        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -494.83],
   ...:    ...:        [u'Insurance', -338.0, -338.0, -338.0, -169.0, 0.0,
   ...:    ...:        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1183.0]]
   ...:

In [2]: from itertools import islice

In [3]: total = [sum(new) for new in islice(zip(*l), 1, None)]

In [4]: total
Out[4]:
[-1083.88, -1083.88, -1083.88, -541.94, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -3793.58]

To include 'total' in the begging as cᴏʟᴅsᴘᴇᴇᴅ kindly noted in comments 要包含'total'在乞讨作为cᴏʟᴅsᴘᴇᴇᴅ在评论中指出的亲切

In [5]: ['total'] + total
Out[6]:
    ['total', -1083.88, -1083.88, -1083.88, -541.94, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -3793.58]

If you want to be really efficient about it, you could use itertools' islice 如果您想真正提高效率,可以使用itertools的islice

from itertools import islice, repeat
s = map(sum, zip(*map(islice, l, repeat(1), repeat(None) ) ) )
total = ['total']
total.extend(s)

Edit: sorry, didn't read the entire context the first time around :) 编辑:对不起,第一次没有阅读整个上下文:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过索引分配列表列表的元素可设置列表的所有首个元素 - Assigning element of list of lists by index sets all first elements of the lists 嵌套列表中的第一个元素的总和 - Sum of first elements in nested lists 删除所有出现的点,除了列表元素中的第一次出现 - Remove all occurrences of dot, except for the first occurence in the elements of a list 增加列表的所有元素,除了 Python 中的第一个元素 - Increase all elements of a list except the first one in Python 给定 3 个列表,找出前两个列表中哪两个元素之和尽可能接近第三个列表中的每个值 - Given 3 lists, find which two elements in the first two lists sum as close as possible to each value in the third list python转置除第一列之外的列表列表 - python transpose list of lists except first column 检查列表列表的第一个元素 - Check first elements of a list of lists 比较两个列表以返回一个列表,其中所有元素都为0,除了那些在保持索引时匹配的列表? - Compare two lists to return a list with all elements as 0 except the ones which matched while keeping index? 如何找到我作为输出得到的几个列表的所有第一个元素的总和 - how to find sum of all first elements of several lists i got as output 如何取除第一个k以外的所有元素 - How to take all elements except the first k
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM