Re: grouping and sorting within groups using another list

2020-09-02 Thread Larry Martell
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, Sept

Re: grouping and sorting within groups using another list

2020-09-02 Thread Peter Otten
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_k

Re: grouping and sorting within groups using another list

2020-09-02 Thread Peter Otten
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

RE: grouping and sorting within groups using another list

2020-09-02 Thread David Raymond
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 lis

grouping and sorting within groups using another list

2020-09-02 Thread Larry Martell
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