Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-16 Thread Jesus Cea
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 13/03/11 13:14, Paul Moore wrote: None of my real code is affected either way, but it seems to me that the removal of the comparison function option was (sadly) a case of purity being allowed to beat practicality. Luckily, adding it back

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-13 Thread Paul Moore
On 13 March 2011 03:00, Raymond Hettinger raymond.hettin...@gmail.com wrote: But in Python 3 this solution is no longer available. How bad is that? I'm not sure. But I'd like to at least get the issue out in the open. Python3.2 should be substantially better in this regard. It no longer

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-13 Thread Daniel Stutzbach
On Sat, Mar 12, 2011 at 3:44 PM, Guido van Rossum gu...@python.org wrote: I recently advised a Googler who was sorting a large dataset and running out of memory. My analysis of the situation was that he was sorting a huge list of short lines of the form shortstring,integer with a key function

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-13 Thread Daniel Stutzbach
On Sat, Mar 12, 2011 at 9:17 PM, Terry Reedy tjre...@udel.edu wrote: But in this case, they are much slower. To be faster, one would need something like key=lambda p,q:p*(lcm//q), where lcm is the least common multiple of of all the q's (denominators). For the example above, lcm = 700. But

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-13 Thread Terry Reedy
On 3/13/2011 2:05 PM, Daniel Stutzbach wrote: On Sat, Mar 12, 2011 at 3:44 PM, Guido van Rossum gu...@python.org mailto:gu...@python.org wrote: I recently advised a Googler who was sorting a large dataset and running out of memory. My analysis of the situation was that he was

[Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Guido van Rossum
I was just reminded that in Python 3, list.sort() and sorted() no longer support the cmp (comparator) function argument. The reason is that the key function argument is always better. But now I have a nagging doubt about this: I recently advised a Googler who was sorting a large dataset and

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Reid Kleckner
They should be able to use a slotted cmp_to_key style class: http://docs.python.org/howto/sorting.html That will allocate 1 Python object with no dict per key, but that might not be good enough. Reid On Sat, Mar 12, 2011 at 3:44 PM, Guido van Rossum gu...@python.org wrote: I was just reminded

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Fredrik Johansson
On Sat, Mar 12, 2011 at 9:44 PM, Guido van Rossum gu...@python.org wrote: I was just reminded that in Python 3, list.sort() and sorted() no longer support the cmp (comparator) function argument. The reason is that the key function argument is always better. But now I have a nagging doubt about

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Nick Coghlan
On Sat, Mar 12, 2011 at 4:50 PM, Reid Kleckner reid.kleck...@gmail.com wrote: They should be able to use a slotted cmp_to_key style class: http://docs.python.org/howto/sorting.html That will allocate 1 Python object with no dict per key, but that might not be good enough. Tuples are already

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Terry Reedy
On 3/12/2011 3:44 PM, Guido van Rossum wrote: I was just reminded that in Python 3, list.sort() and sorted() no longer support the cmp (comparator) function argument. The reason is that the key function argument is always better. But now I have a nagging doubt about this: I recently advised a

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Reid Kleckner
On Sat, Mar 12, 2011 at 4:58 PM, Nick Coghlan ncogh...@gmail.com wrote: On Sat, Mar 12, 2011 at 4:50 PM, Reid Kleckner reid.kleck...@gmail.com wrote: They should be able to use a slotted cmp_to_key style class: http://docs.python.org/howto/sorting.html That will allocate 1 Python object

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Eugene Toder
Can sort have an option (and/or try to figure it itself) to calculate key for every comparison instead of caching them? This will have the same memory requirements as with cmp, but doesn't require rewriting code if you decide to trade speed for memory. Will this be much slower than with cmp? If

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Martin v. Löwis
Am 12.03.11 16:58, schrieb Nick Coghlan: On Sat, Mar 12, 2011 at 4:50 PM, Reid Klecknerreid.kleck...@gmail.com wrote: They should be able to use a slotted cmp_to_key style class: http://docs.python.org/howto/sorting.html That will allocate 1 Python object with no dict per key, but that might

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Peter Otten
Guido van Rossum wrote: I was just reminded that in Python 3, list.sort() and sorted() no longer support the cmp (comparator) function argument. The reason is that the key function argument is always better. But now I have a nagging doubt about this: I recently advised a Googler who was

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Nick Coghlan
On Sat, Mar 12, 2011 at 5:41 PM, Martin v. Löwis mar...@v.loewis.de wrote: Why not? IIUC, the current key function creates three objects: the tuple, the short string, and the int. With the class Yeah, I misread the example. Using cmp_to_key would indeed save quite a lot of memory in this case.

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Martin v. Löwis
But in Python 3 this solution is no longer available. How bad is that? I'm not sure. But I'd like to at least get the issue out in the open. Rather than reintroducing cmp=, I'd add a cached=True parameter. If this is set to False, the key results wouldn't be put into a list, but recreated every

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Glenn Linderman
On 3/12/2011 1:55 PM, Fredrik Johansson wrote: Consider sorting a list of pairs representing fractions. This can be done easily in Python 2.x with the comparison function lambda (p,q),(r,s): cmp(p*s, q*r). In Python 2.6, this is about 40 times faster than using fractions.Fraction as a key

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Martin v. Löwis
Am 12.03.11 18:00, schrieb Glenn Linderman: On 3/12/2011 1:55 PM, Fredrik Johansson wrote: Consider sorting a list of pairs representing fractions. This can be done easily in Python 2.x with the comparison function lambda (p,q),(r,s): cmp(p*s, q*r). In Python 2.6, this is about 40 times

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Fredrik Johansson
On Sun, Mar 13, 2011 at 12:41 AM, Martin v. Löwis mar...@v.loewis.de wrote: Am 12.03.11 18:00, schrieb Glenn Linderman:  On 3/12/2011 1:55 PM, Fredrik Johansson wrote: Consider sorting a list of pairs representing fractions. This can be done easily in Python 2.x with the comparison function

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Terry Reedy
On 3/12/2011 5:09 PM, Reid Kleckner wrote: On Sat, Mar 12, 2011 at 4:58 PM, Nick Coghlanncogh...@gmail.com wrote: On Sat, Mar 12, 2011 at 4:50 PM, Reid Klecknerreid.kleck...@gmail.com wrote: They should be able to use a slotted cmp_to_key style class:

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Steven D'Aprano
Fredrik Johansson wrote: Consider sorting a list of pairs representing fractions. This can be done easily in Python 2.x with the comparison function lambda (p,q),(r,s): cmp(p*s, q*r). In Python 2.6, this is about 40 times faster than using fractions.Fraction as a key function. [steve@sylar

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Glenn Linderman
On 3/12/2011 2:09 PM, Terry Reedy wrote: I believe that if the integer field were padded with leading blanks as needed so that all are the same length, then no key would be needed. Did you mean that if the integer field were converted to string and padded with leading blanks...? Otherwise

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Terry Reedy
On 3/12/2011 8:28 PM, Steven D'Aprano wrote: Fredrik Johansson wrote: Consider sorting a list of pairs representing fractions. This can be done easily in Python 2.x with the comparison function lambda (p,q),(r,s): cmp(p*s, q*r). In Python 2.6, this is about 40 times faster than using

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Martin v. Löwis
[steve@sylar ~]$ python2.7 -m timeit -s L = [(1,2), (3,4), (0,5), (9,100), (3,7), (2,8)] sorted(L, lambda (p,q),(r,s): cmp(p*s, q*r)) 1 loops, best of 3: 25.1 usec per loop [steve@sylar ~]$ python2.7 -m timeit -s L = [(1,2), (3,4), (0,5), (9,100), (3,7), (2,8)] -s from fractions import

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Raymond Hettinger
On Mar 12, 2011, at 3:44 PM, Guido van Rossum wrote: I was just reminded that in Python 3, list.sort() and sorted() no longer support the cmp (comparator) function argument. The reason is that the key function argument is always better. But now I have a nagging doubt about this: I

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Terry Reedy
On 3/12/2011 8:47 PM, Glenn Linderman wrote: On 3/12/2011 2:09 PM, Terry Reedy wrote: I believe that if the integer field were padded with leading blanks as needed so that all are the same length, then no key would be needed. Did you mean that if the integer field were converted to string and

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Glenn Linderman
On 3/12/2011 7:21 PM, Terry Reedy wrote: (Ok, I assumed that the 'word' field does not include any of !#$%'()*+. If that is not true, replace comma with space or even a control char such as '\a' which even precedes \t and \n.) OK, I agree the above was your worst assumption, although you need

Re: [Python-Dev] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Terry Reedy
On 3/12/2011 10:52 PM, Glenn Linderman wrote: On 3/12/2011 7:21 PM, Terry Reedy wrote: The safest such character is \0,\ Works fine in Python. unless you are coding in C, Then \01 is next best. I wouldn't have called you on this, except that it really is important not to give people