grouping and sorting within groups using another list

Peter Otten __peter__ at web.de
Wed Sep 2 14:26:16 EDT 2020


Larry Martell wrote:

> I have a list of tuples, and I want to group them by 3 items (0, 3, 4)
> and then within each group sort the data by a 4th item (6) using a
> sort order from another list. The list is always ordered by the 3
> grouping items.

>From your description I deduced

from itertools import chain, groupby
from operator import itemgetter
from pprint import pprint

rows = [
    ('a', 'b', 'c', 'd', 'e', 'f', 'blue', ...),
    ('a', 'x', 'y', 'd', 'e', 'f', 'green', ...),
    ('a', 'q', 'w', 'd', 'e', 'f', 'white', ...),
    ('p', 'x', 'y', 'd', 'e', 'f', 'white', ...),
    ('p', 'x', 'y', 'd', 'e', 'f', 'blue', ...),
    ('p', 'x', 'y', 'd', 'e', 'f', 'green', ...),
    ('z', 'x', 'y', 'm', 'n', 'o', 'green', ...),
    ('z', 'x', 'y', 'm', 'n', 'o', 'white', ...),
    ('z', 'x', 'y', 'm', 'n', 'o', 'blue', ...),
]

sort_list = ['blue', 'white', 'green']

wanted = [
    ('a', 'b', 'c', 'd', 'e', 'f', 'blue', ...),
    ('a', 'x', 'y', 'd', 'e', 'f', 'white', ...),
    ('a', 'q', 'w', 'd', 'e', 'f', 'green', ...),
    ('p', 'x', 'y', 'd', 'e', 'f', 'blue', ...),
    ('p', 'x', 'y', 'd', 'e', 'f', 'white', ...),
    ('p', 'x', 'y', 'd', 'e', 'f', 'green', ...),
    ('z', 'x', 'y', 'm', 'n', 'o', 'blue', ...),
    ('z', 'x', 'y', 'm', 'n', 'o', 'white', ...),
    ('z', 'x', 'y', 'm', 'n', 'o', 'green', ...),
]

group_key = itemgetter(0, 3, 4)


def sort_key(row, lookup={k: i for i, k in enumerate(sort_list)}):
    return lookup[row[6]]


result = list(
    chain.from_iterable(
        sorted(group, key=sort_key)
        for _key, group in groupby(rows, key=group_key)
    )
)
pprint(wanted)
pprint(result)
assert result == wanted


Unfortunately the assertion fails. I'm not sure whether I misunderstood your 
description or if your sample data is incorrect.

> 
> For example, if I have this list:
> rows =
> [('a', 'b', 'c', 'd', 'e', 'f', 'blue', ....),
>  ('a', 'x', 'y', 'd', 'e', 'f', 'green', ....),
>  ('a', 'q', 'w', 'd', 'e', 'f', 'white', ....),
>  ('p', 'x', 'y', 'd', 'e', 'f', 'white', ....),
>  ('p', 'x', 'y', 'd', 'e', 'f', ' 'blue', ...),
>  ('p', 'x', 'y', 'd', 'e', 'f', ' 'green', ...),
>  ('z', 'x', 'y', 'm', 'n', 'o', 'green, ...),
>  ('z', 'x', 'y', 'm', 'n', 'o', 'white, ...),
>  ('z', 'x', 'y', 'm', 'n', 'o', 'blue, ...),
> ]
> 
> and I have a list:
> 
> sort_list = ['blue', 'white', 'green']
> 
> Then the result list would be:
> 
> [('a', 'b', 'c', 'd', 'e', 'f', 'blue', ....),
>  ('a', 'x', 'y', 'd', 'e', 'f', 'white', ....),
>  ('a', 'q', 'w', 'd', 'e', 'f', 'green', ....),
>  ('p', 'x', 'y', 'd', 'e', 'f', 'blue', ....),
>  ('p', 'x', 'y', 'd', 'e', 'f', ' 'white', ...),
>  ('p', 'x', 'y', 'd', 'e', 'f', ' 'green', ...),
>  ('z', 'x', 'y', 'm', 'n', 'o', 'blue, ...),
>  ('z', 'x', 'y', 'm', 'n', 'o', 'white, ...),
>  ('z', 'x', 'y', 'm', 'n', 'o', 'green, ...),
> ]
> 
> Been trying to do with using groupby but have not been successful.




More information about the Python-list mailing list