grouping and sorting within groups using another list
Larry Martell
larry.martell at gmail.com
Wed Sep 2 13:54:42 EDT 2020
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.
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