您似乎没有足够准确地描述您的问题。
您的真正问题只能从您对@Blender 的问题和答案的评论中掌握。他对问题的好解决方案不会立即适用于我所理解的问题案例,......但几乎。
这是一种扩展以满足您的需求的方法:
# some toy example data - I understand you want the first 2 sub_list
# to be "merged" because BOTH strings in pos 0 and 2 match
data = [['42x120x1800', 50, '50x90x800', 60],
['42x120x1800', 8, '50x90x800', 10],
['2x10x800', 5, '5x9x80', 6]]
from collections import defaultdict
# I'm using a lambda to initialize the items of the dict
# to a two-element list of zeros
d = defaultdict(lambda :[0, 0])
for sub_list in data:
key = (sub_list[0], sub_list[2])
d[key][0] += sub_list[1]
d[key][1] += sub_list[3]
for key in d:
print key, d[key]
# ('2x10x800', '5x9x80') [5, 6]
# ('42x120x1800', '50x90x800') [58, 70]
如果你想回到数据的初始表示:
new_data = [[key[0], val[0], key[1], val[1]] for key, val in d.iteritems()]
# [['2x10x800', 5, '5x9x80', 6], ['42x120x1800', 58, '50x90x800', 70]]