当前位置 博文首页 > python如何把列表里面的部分值相加??_weixin_39538607的博客:
展开全部
程序代码如下:62616964757a686964616fe78988e69d8331333337623530# -*- coding: cp936 -*-
mobile = [
['apple', 'android', 'web', 1, 11],
['apple', 'android', 'web', 2, 22],
['apple', 'ios', 'web', 3, 33],
['apple', 'ios', 'web', 4, 44]]
out=[]
temp=""
index=-1
for ii in range(len(mobile)):
tempM=mobile[ii]
if temp != tempM[1]:
out.append(tempM)
temp = tempM[1]
index=index+1
else:
out[index][3]=out[index][3]+tempM[3]
out[index][4]=out[index][4]+tempM[4]
print out
tempout=[]
tempout.append(out[0][0])
tempout.append('total')
tempout.append(out[0][2])
tempout.append(out[0][3])
tempout.append(out[0][4])
for iii in range(1,len(out)):
tempout[3]=tempout[3]+out[iii][3]
tempout[4]=tempout[4]+out[iii][4]
tempout[3]=str(tempout[3])
tempout[4]=str(tempout[4])
out.append(tempout)
print out
cs