简体   繁体   中英

Python Group List of Lists

I have a list of lists in the following format:

[['Sarah', '12', 'Chocolate'],
 ['Anders', '11', 'Vanilla'],
 ['Sarah', '13', 'Strawberry'],
 ['John', '11', 'None'],
 # ...
]

And I want to group the sublists as follows:

[['Sarah', '12', 'Chocolate', '13', 'Strawberry'],
 ['Anders', '11', 'Vanilla'],
 ['John', '11', 'None'],
 # ...
]

Where I group by the first item of the sublists and order by the second (so Sarahs with age 12 come before Sarahs with age 13).

How to do this?

You didn't show any code, so I won't give a complete solution.

One good data structure would be to use a dict , with names as keys and a list of tuples as values:

data =[['Sarah', '12', 'Chocolate'],
    ['Anders', '11', 'Vanilla'],
    ['Sarah', '13', 'Strawberry'],
    ['John', '11', 'None']]

grouped = {}

for name, x, y in data:
    grouped.setdefault(name, []).append((x,y))

print(grouped)
# {'Sarah': [('12', 'Chocolate'), ('13', 'Strawberry')], 'Anders': [('11', 'Vanilla')], 'John': [('11', 'None')]}

You'd just need to sort the values.

A few possible solutions jump to mind, but here's one. Read through the comments and feel free to ask questions:

from collections import defaultdict

l = [
        ['Sarah', '12', 'Chocolate'],
        ['Anders', '11', 'Vanilla'],
        ['Sarah', '13', 'Strawberry'],
        ['John', '11', 'None'],
]

d = defaultdict(list)

for entry in l:
    d[entry[0]].append(entry[1:])

# Here d looks something like this:
# {'Sarah': [['12', 'Chocolate'], ['13', 'Strawberry']], ...}

result = [
    # 1. Sort the list by the first element of each sublist (parsed as an int).
    # 2. Flatten the result as in https://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python/952952#952952.
    # 3. Prepend the key (name).
    [k] + [item for sublist in sorted(v, key=lambda x: int(x[0])) for item in sublist]
    for k,v in d.items()
]

print(result)

# Output: [['Sarah', '12', 'Chocolate', '13', 'Strawberry'], ['Anders', '11', 'Vanilla'], ['John', '11', 'None']]

You can try this :

import itertools


list_1=[['Sarah', '12', 'Chocolate'],
       ['Anders', '11', 'Vanilla'],
       ['Sarah', '13', 'Strawberry'],
       ['John', '11', 'None']]

repeated_list=[]
unique_list=[]
for item_1,item_2 in itertools.combinations(list_1,2):
    if item_1[0]==item_2[0]:
        repeated_list.append(item_1+item_2)

    else:

        for item_3 in repeated_list:
            if item_2[0] not in item_3:
                if item_2 not in unique_list:
                    unique_list.append(item_2)

            elif item_1[0] not in item_3:
                if item_1 not in unique_list:
                    unique_list.append(item_1)


print(unique_list+repeated_list)

Output:

[['John', '11', 'None'], ['Anders', '11', 'Vanilla'], ['Sarah', '12', 'Chocolate', 'Sarah', '13', 'Strawberry']]

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