I've a 2D array and would like to combine partially similar items.
[
[Red,Blue,Yellow,5]
[Red,Blue,Yellow,10]
[Red,Blue,Green,5]
[Red,Blue,Green,5]
[Red,Blue,Orange,5]
[Red,Blue,Violet,5]
]
I want it to become like this, trimming and creating a unique array
[
[Red,Blue,Yellow,15]
[Red,Blue,Green,10]
[Red,Blue,Orange,5]
[Red,Blue,Violet,5]
]
Edit1: Here's what i did. I've only the basics and would like to have a better version.
newTaskList = []
totalTaskList = []
totalValueList = []
finalTaskList = []
for taskIndex, taskList in enumerate(readTaskList):
newTaskList = []
newTaskList.append(taskList[0])
newTaskList.append(taskList[1])
newTaskList.append(taskList[2])
newTaskList.append(taskList[4])
if(newTaskList not in totalTaskList):
totalTaskList.append(newTaskList)
totalValueList.append(float(taskList[3]))
else:
for itemIndex, itemList in enumerate(totalTaskList):
if(itemList[0] == taskList[0] and itemList[1] == taskList[1] and itemList[2] == taskList[2] and itemList[3] == taskList[4]):
totalValueList[itemIndex] += float(taskList[3])
for taskIndex, task in enumerate(totalTaskList):
for workType in workTypeList:
newWorkTypeItem = task[2].replace(" ","_").split("_")
if len(newWorkTypeItem) > 1:
task[2] = newWorkTypeItem[0] + " " + newWorkTypeItem[1]
if(task[1] == workType[0] and task[2] == workType[1]):
task[2] = workType[2]
break
task.append(totalValueList[taskIndex])
finalTaskList.append(task)
defaultdict, use a tuple of the colors as a key, and iterate through, adding all the values with the same keys together.