Re: sorting on keys in a list of dicts

2005-01-08 Thread Peter Hansen
Nick Coghlan wrote: Stability in sorting is a property not to be sneezed at - it means switching to sorting by a second key gives the effect of sort by key 1, then by key 2, whereas that doesn't hold with an unstable sort Assuming key 1 refers to the first key, and key 2 to the second key, then

Re: sorting on keys in a list of dicts

2005-01-07 Thread It's me
What does it mean by stability in sorting? Can somebody please give a sample for using the code posted? I am a little lost here and I like to know more about the use of keys Thanks, Nick Coghlan [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Jeff Shannon wrote: I suppose that

Re: sorting on keys in a list of dicts

2005-01-07 Thread Craig Ringer
On Sat, 2005-01-08 at 00:26, It's me wrote: What does it mean by stability in sorting? If I understand correctly, it means that when two sorts are performed in sequence, the keys that are equal to the second sort end up ordered the way they were left by the first sort. I'm far from certain of

Re: sorting on keys in a list of dicts

2005-01-07 Thread Nick Coghlan
It's me wrote: What does it mean by stability in sorting? Can somebody please give a sample for using the code posted? I am a little lost here and I like to know more about the use of keys It's the jargon for what Jeff said - if you are sorting by some value calculated from each list entry,

Re: sorting on keys in a list of dicts

2005-01-07 Thread Tim Peters
[Nick Coghlan] ... Python 2.3 has a stable sort, and Python 2.4 brought the guarantee that it shall remain that way. I'm not sure about Python 2.2 and earlier. No list.sort() implementation before 2.3 was stable. It was confusing, though, because the samplesort/binary_insertion_sort hybrid

Re: sorting on keys in a list of dicts

2005-01-07 Thread Scott David Daniels
Jeff Shannon wrote: Jp Calderone wrote: L2 = [(d[key], i, d) for (i, d) in enumerate(L)] L2.sort() L = [d for (v, i, d) in L2] Out of curiosity, any reason that you're including the index? Others have already remarked that this preserves sort stability (which is, in fact a lovely

Re: sorting on keys in a list of dicts

2005-01-07 Thread Jeff Shannon
Nick Coghlan wrote: Jeff Shannon wrote: I suppose that your version has the virtue that, if the sortkey value is equal, items retain the order that they were in the original list, whereas my version will sort them into an essentially arbitrary order. Is there anything else that I'm missing

Re: sorting on keys in a list of dicts

2005-01-07 Thread Nick Coghlan
Jeff Shannon wrote: Agreed. I'd started typing before I realized that it'd provide a stable sort, which pretty much answered my own question, but decided to send it anyhow in case I'd missed anything else... :) And it turns out we both missed the fact that it avoids comparing the dictionaries

Re: sorting on keys in a list of dicts

2005-01-07 Thread Carl Banks
Jeff Shannon wrote: Jp Calderone wrote: L2 = [(d[key], i, d) for (i, d) in enumerate(L)] L2.sort() L = [d for (v, i, d) in L2] Out of curiosity, any reason that you're including the index? I'd have expected to just do L2 = [(d[key], d) for d in L] L2.sort()

sorting on keys in a list of dicts

2005-01-06 Thread J Berends
Suppose I have a list of dictionaries and each dict has a common keyname with a (sortable) value in it. How can I shuffle their position in the list in such way that they become sorted. Maybe I am doing it wrongly and you would say: why don't you get yourself a (slimmed-down) implementation

Re: sorting on keys in a list of dicts

2005-01-06 Thread Jp Calderone
On Thu, 06 Jan 2005 15:31:22 +0100, J Berends [EMAIL PROTECTED] wrote: Suppose I have a list of dictionaries and each dict has a common keyname with a (sortable) value in it. How can I shuffle their position in the list in such way that they become sorted. In Python 2.4, import

Re: sorting on keys in a list of dicts

2005-01-06 Thread Paul Rubin
J Berends [EMAIL PROTECTED] writes: Suppose I have a list of dictionaries and each dict has a common keyname with a (sortable) value in it. How can I shuffle their position in the list in such way that they become sorted. Do I understand the question right? Can't you just say

Re: sorting on keys in a list of dicts

2005-01-06 Thread J Berends
Jp Calderone wrote: On Thu, 06 Jan 2005 15:31:22 +0100, J Berends [EMAIL PROTECTED] wrote: Suppose I have a list of dictionaries and each dict has a common keyname with a (sortable) value in it. How can I shuffle their position in the list in such way that they become sorted. In Python 2.4,

Re: sorting on keys in a list of dicts

2005-01-06 Thread Jeff Shannon
Jp Calderone wrote: L2 = [(d[key], i, d) for (i, d) in enumerate(L)] L2.sort() L = [d for (v, i, d) in L2] Out of curiosity, any reason that you're including the index? I'd have expected to just do L2 = [(d[key], d) for d in L] L2.sort() L = [d for (v, d) in L2] I