954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Merge lists when certain list items match

Say I have a list of lists as follows (which I do):

[['Value1', 'Value2', 'Value3'], ['Value1', 'Value2', 'Value3'], ['Value1', 'Value2', 'Value3'], ['Value1', 'Value2', 'Value3']]


What I want to accomplish is if in any list Value1 & Value2 equal the Value1 & Value2 of any other list, I want to merge those two lists so only one is preserved and the two values for Value3 are summed (then append the list back to the list of lists).

I'm thinking along the lines of looping through the lists within the list, comparing the lists for matches where[i] & [i + 1] are equal, and somehow merging them so that [i + 2] is summed. Anyone have an idea?

fingerpainting
Newbie Poster
12 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

Your description is scrambled, do you want to remove duplicates from list?
Like:

[['Value1', 'Value2', 'Value3'], ['Value1', 'Value2', 'Value3'], ['Value1', 'Value2', 'Value3'], ['Value1', 'Value2', 'Value3']]
becomes     [['Value1', 'Value2', 'Value3']]
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

Sorry, I want

[['AAA', 'BBB', '111'], ['CCC', 'DDD', '222'], ['CCC', 'DDD', '333'], ['EEE', 'FFF', '444']]


to become

[['AAA', 'BBB', '111'], ['CCC', 'DDD', '555'], ['EEE', 'FFF', '444']]
fingerpainting
Newbie Poster
12 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

Use sorted() and itertools.groupby()

from itertools import groupby

data = """
BBB AAA 111
CCC DDD 222
BBB AAA 333
EEE FFF  77
CCC DDD 99
"""

def pair(item):
    return tuple(item[:2])

L = [x.strip().split() for x in data.strip().splitlines()]
print L
R = [ [x, y, str(sum(int(z[2]) for z in g))] for (x, y), g in groupby(sorted(L, key=pair), key=pair) ]
print R

""" my output -->
[['BBB', 'AAA', '111'], ['CCC', 'DDD', '222'], ['BBB', 'AAA', '333'], ['EEE', 'FFF', '77'], ['CCC', 'DDD', '99']]
[['BBB', 'AAA', '444'], ['CCC', 'DDD', '321'], ['EEE', 'FFF', '77']]
"""
Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

Slightly differently variant of Gribouillis' code as keeping the numbers as strings does not make much sense, using dictionary and set of keys, result as sorted list of dictionary items (not as requested):

data = """
BBB AAA 111
CCC DDD 222
BBB AAA 333
EEE FFF  77
CCC DDD 99
"""

def pair(item):
    return tuple(item[:2]), item[-1]

data_list = [pair(x.strip().split()) for x in data.strip().splitlines()]

# format with string for of integers
# is strange for this application, result as integer
summed = dict((item,sum(int(n) for key, n in data_list if key==item))
             for item in set(key for key,n in data_list))
# slightly different result format that makes more sense
print(sorted(summed.items()))
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

Still another version which doesn't need to sort the items (L could be any iterable)

from functools import reduce

def my_add(D, (x, y, z)):
    D[(x, y)] = int(z) + D.get((x, y), 0)
    return D

print reduce(my_add, L, dict())

""" my output -->
{('EEE', 'FFF'): 77, ('BBB', 'AAA'): 444, ('CCC', 'DDD'): 321}
"""
Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

Many thanks both. I tried the first one and it works, so I'm solving the thread. I am looking at the sorted function to see if I could preserve the original sorting, and I'll explore the other two solutions as well. Thanks again.

fingerpainting
Newbie Poster
12 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 
Many thanks both. I tried the first one and it works, so I'm solving the thread. I am looking at the sorted function to see if I could preserve the original sorting, and I'll explore the other two solutions as well. Thanks again.


For keeping first occurance order look for OrderedDict

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: