Вы можете использовать setdefault вместе с некоторым индексированием для получения желаемых результатов.
ls=[['01',2,3,4],['02',5,2,4],['03',2,6,4],['01',1,3,4]]
totals = {}
for element in ls:
key ,array = element[0],element[1:]
for index in range(len(array)):
totals.setdefault(key,[0]*len(array))[index] += array[index]
result = []
for key,value in totals.items():
result.append([key] + value)
print(result)
# you can sort the result to get exactly as you wanted
# you dont need this step if the dictionary is already sorted like in python3
result.sort(key=lambda x:x[0])
print(result)
Редактировать, чтобы обрабатывать случай, когда символы приходят и первый символ выбирается
ls=[['01',2,'a',4],['02',5,2,4],['03',2,3,4],['01',1,'asd',4]]
totals = {}
for element in ls:
key ,array = element[0],element[1:]
for index in range(len(array)):
if key not in totals:
totals.setdefault(key,array)
elif (type(totals[key][index]) == "int") and type(array[index] == "int"):
totals[key][index] += array[index]
result = []
for key,value in totals.items():
result.append([key] + value)
print(result)
# you can sort the result to get exactly as you wanted
# you dont need this step if the dictionary is already sorted like in python3
result.sort(key=lambda x:x[0])
print(result)
ВЫХОД
В питоне 2
[['02', 5, 2, 4], ['03', 2, 6, 4], ['01', 3, 6, 8]]
[['01', 3, 6, 8], ['02', 5, 2, 4], ['03', 2, 6, 4]]
В питоне 3
[['01', 3, 6, 8], ['02', 5, 2, 4], ['03', 2, 6, 4]]
[['01', 3, 6, 8], ['02', 5, 2, 4], ['03', 2, 6, 4]]