grouping and sorting within groups using another list
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. -- https://mail.python.org/mailman/listinfo/python-list
RE: grouping and sorting within groups using another list
Would it be something as simple as: rows.sort(key = lambda x: (x[0], x[3], x[4], sort_list.index(x[6]))) -Original Message- From: Python-list On Behalf Of Larry Martell Sent: Wednesday, September 2, 2020 1:55 PM To: Python Subject: grouping and sorting within groups using another list 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. -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: grouping and sorting within groups using another list
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. -- https://mail.python.org/mailman/listinfo/python-list
Re: grouping and sorting within groups using another list
Peter Otten wrote: > 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) > ) > ) It just occured to me that if the above code is what you want you can omit the groupby and sort with a combined key: sorted(rows, key=lambda row: (group_key(row), sort_key(row))) (The lambda is for illustration, you could of course merge the two key functions into one) -- https://mail.python.org/mailman/listinfo/python-list
Re: grouping and sorting within groups using another list
On Wed, Sep 2, 2020 at 11:22 AM David Raymond wrote: > > Would it be something as simple as: > > rows.sort(key = lambda x: (x[0], x[3], x[4], sort_list.index(x[6]))) This is perfect - thanks! > -Original Message- > From: Python-list > On Behalf Of Larry Martell > Sent: Wednesday, September 2, 2020 1:55 PM > To: Python > Subject: grouping and sorting within groups using another list > > 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. -- https://mail.python.org/mailman/listinfo/python-list