简体   繁体   中英

Python List of Lists Merging/removing duplicates

I've got a list of lists with differing properties:

list = [['jake', '100', 'science'], ['sam', '200', 'math'], ['jake', '100', 'science'], ['sam', '200', 'science']]

the list represents students' names, test scores, and subject respectively.

This is the desired output: list = [['jake', '200', 'science'], ['sam', '200', 'math'], ['sam', '200', 'science']]

The scores for Jake's two science scores were added together (100+100 = 200) and list duplicate was removed.

I want to add test scores belonging to the same person and same subject and keep the others separate. Any idea how I can do this seamlessly?

You can use groupby from itertools to solve this nicely.

You'll need to both sort and group using the key lambda x: (x[0], x[2]) which makes sure the sorting and grouping is done by student & subject.

from itertools import groupby
list = [
    ['jake', '100', 'science'],
    ['sam', '200', 'math'],
    ['jake', '100', 'science'],
    ['sam', '200', 'science']
]

sorted_list = sorted(list, key=lambda x: (x[0], x[2]))
grouped_list = groupby(sorted_list, key=lambda x: (x[0], x[2]))

output_list = [[student, str(sum(int(score[1]) for score in scores)), subject]
    for (student, subject), scores in grouped_list]

Gives as desired:

[['jake', '200', 'science'], ['sam', '200', 'math'], ['sam', '200', 'science']]

Something along the lines of

from functools import reduce

list = [['jake', '100', 'science'], ['sam', '200', 'math'], ['jake', '100', 'science'], ['sam', '200', 'science']]


def reducer(acc, item):
    key = f'{item[0]}_{item[2]}'
    if key not in acc:
        item[1] = int(item[1])
        acc[key] = item
    else:
        acc[key][1] = acc[key][1] + int(item[1])

    return acc

new_list = reduce(reducer, list, {}).values()


print(new_list)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM