简体   繁体   中英

group list of tuples to list of tuples with lists in python

I have a list of tuples, each tuple has patient and visit, patient can have several visits

I want to get list of patient and for every patient the list of their visits

for example

[(patient1, visit), (patient2, visit), (patient1, visit)]

To

[(patient1, [visit, visit]), (patient2, [visit])]

I tried javascript's reduce function approach, but I can't really understand how I can do it in python

The defaultdict approach is the standard way and has linear complexity. You can also just use a common dict and dict.setdefault

d = {}
for patient, visit in data:
    d.setdefault(patient, []).append(visit)
[*d.items()]
# [('patient1', ['visit', 'visit']), ('patient2', ['visit'])]

For a one-line approach (excluding imports) - albeit only log-linear, you can use itertools.groupby :

from itertools import groupby
from operator import itemgetter as ig

[(k, [*map(ig(1), g)]) for k, g in groupby(sorted(data), key=ig(0))]
# [('patient1', ['visit', 'visit']), ('patient2', ['visit'])]

Some useful docs:

You can use a collections.defaultdict in the following way:

from collections import defaultdict

d = defaultdict(list)
for patient, visit in data:
    d[patient].append(visit)

Example Using itertools.groupby

from itertools import groupby

# Example data
records = [('bill', '1/1/2021'), ('mary', '1/2/2021'), ('janet', '1/3/2021'), ('bill', '3/5/2021'), ('mary', '4/25/2021')]

# Group visits by patient names
g = groupby(sorted(records), lambda kv: kv[0])  # Group based upon first element of tuples (i.e. name)
                                                # Sort so names are adjacent for groupby
    
# Using list comprehension on groupings to provided desired tuples
result =  [(name, [d[1] for d in visit]) for name, visit in g]

Above code as a one-liner

result = [(name, [d[1] for d in visit]) for name, visit in  groupby(sorted(records), lambda kv: kv[0])]

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