1

I have a list of lists in this format:

[[<image object1>, source1 , version1],[<image object2>, source2 , version2]...]

I need to compare each list and construct a new list of lists that contains unique source values. When there are duplicated source values, I need to pick the list with the highest version value.

Also, is this the proper data structure I should use?

2 Answers 2

4

You can use itertools.groupby and the max function for that:

>>> lst = [['foo', 1, 2], ['asdf', 2, 5], ['bar', 1, 3]]
>>> import itertools as it
>>> from operator import itemgetter
>>> [max(items, key=itemgetter(2)) 
     for _,items in it.groupby(sorted(lst, key=itemgetter(1)), key=itemgetter(1))]
[['bar', 1, 3], ['asdf', 2, 5]]
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming that all of your sublists have that same three item structure, that seems like a fairly sensible data structure to use, since you can always access the image object, source and version with indexes [0], [1] and [2].

This code makes the sources the keys of a dictionary, and the sublists the values of those keys.

bigList = [['foo', 1, 2], ['asdf', 2, 5], ['bar', 1, 3]]
uniqueSources = {}
for sublist in bigList:
    currentSource = sublist[1]
    if currentSource in uniqueSources:
        if sublist[2] > uniqueSources[currentSource][2]:
            uniqueSources[currentSource] = sublist
    else: uniqueSources[currentSource] = sublist
dupesRemoved = list(uniqueSources.values())
print(dupesRemoved)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.