python 一个列表sum

lutaotony 2014-01-26 11:37:57
res =[]
newres=[]
res.append(1,'test',3)
res.appden(1,'test',4)
res.appden(2,'aa',5)
res.appden(2,'aa',6)
for temp in res:
......
想得到结果是
newres[(1,'test',7),(2,'aa'11)]
初学,请教,谢谢!
...全文
316 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lutaotony 2014-02-15
  • 打赏
  • 举报
回复
先谢谢了,最近在改C#有时间去试试!
sleep365 2014-01-31
  • 打赏
  • 举报
回复
楼上正解,写的很好,学习了。!
fly0snow 2014-01-26
  • 打赏
  • 举报
回复
def myCompare(a,b):
	return (cmp(a[0],b[0]) or cmp(a[1],b[1]))

def convert(source,target):
	source.sort();
	for item in source:
		if len(target) == 0:
			target.append(list(item))
		elif myCompare(item,target[-1]) == 0:
			target[-1][2] += item[2]
		else:
			target.append(list(item))

>>> convert(res,newRes)
>>> newRes
[[1, 'test', 7], [2, 'aa', 11]]

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧