[英]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.