0

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)
3
  • 1
    Show what you've tried already Commented May 30, 2018 at 8:34
  • Please check [SO]: How to create a Minimal, Complete, and Verifiable example (mcve). Hint: this could probably be done in one line of code (excepting imports) using itertools module. Commented May 30, 2018 at 8:35
  • I'd create a defaultdict, use a tuple of the colors as a key, and iterate through, adding all the values with the same keys together. Commented May 30, 2018 at 8:36

2 Answers 2

1

Using collections Module.

Ex:

from collections import defaultdict
l = [
['Red','Blue','Yellow',5],
['Red','Blue','Yellow',10],
['Red','Blue','Green',5],
['Red','Blue','Green',5],
['Red','Blue','Orange',5],
['Red','Blue','Violet',5],
]

d = defaultdict(int)
for i in l:
    d[tuple(i[:3])] += i[-1]
print [list(k) + [v] for k,v in d.items()]

Output:

[['Red', 'Blue', 'Orange', 5], ['Red', 'Blue', 'Yellow', 15], ['Red', 'Blue', 'Violet', 5], ['Red', 'Blue', 'Green', 10]]
Sign up to request clarification or add additional context in comments.

2 Comments

I knew there's an awesome way in doing it in python and here it is! Thank you so much.
A thing to mention is that the result wouldn't appear in the same order as the original list but if that is not an issue for your purpose you are fine.
1

You can use defaultdict.

result = defaultdict(int)
for col1, col2, col3, value in my_list:
   result[(col1, col2, col3)] += value
result = [list(key) + [value] for key,value in result.items()]
print(result)

Output

[['Red', 'Blue', 'Yellow', 15], ['Red', 'Blue', 'Green', 10], ['Red', 'Blue', 'Orange', 5], ['Red', 'Blue', 'Violet', 5]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.