给datatable增加行总和和列总和
[[["TYPE",""],["April",""],["May",""]],
[["RED",""],[15 , "item3,item9"] [9 , "item7,item3"]],
[["BLUE",""],[7 , "item7,item8"] [2 , "item1,item8"]],
[["GREEN",""],[10 , "item5,item8"] [7 , "item3,item7"]]
]
希望生成类似于
[["RED",""],[15 , "item3,item9"] [9 , "item7,item3"],[24,"item3,item7,item9"]],
[["BLUE",""],[7 , "item7,item8"] [2 , "item1,item8"]],[9,"item1,item7,item8"],
[["GREEN",""],[10 , "item5,item8"] [7 , "item3,item7"],[17,"item3,item5,item7"]]
[["TOTAL",""],[32 , "item5,item8"] [18 , "item3,item7"],[50,"item3,item5,item8"]]]
我写了这样
def helper(x,y):
return [x[0]+y[0],x[1]+y[1]]
def colum(x,y):
return [helper(x[i],y[i]) for i in range(len(x))]
for line in table[1:]:
line.append(reduce(helper,line[1:]))
table.append(reduce(colum,table[1:]))
table[0].append(["RowTotal",""])
table[-1][0]=["TOTAL",""]
for line in table:
print(line)
可以写的更加简单吗?觉的可以加个map,省去定义colum函数,但没有成功。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论