Re: Q: sort's key and cmp parameters

2009-10-07 Thread Paul Rubin
Bearophile bearophileh...@lycos.com writes: sorting, and something that's surely not bug-prone. In such situation having a 'key' argument is *better*. Such sort can be stable. Nothing stops comparison sorting from being stable. Since the rest of your post seems premised on the opposite, I

Re: Q: sort's key and cmp parameters

2009-10-07 Thread Steven D'Aprano
On Tue, 06 Oct 2009 23:20:13 -0700, Paul Rubin wrote: Bearophile bearophileh...@lycos.com writes: sorting, and something that's surely not bug-prone. In such situation having a 'key' argument is *better*. Such sort can be stable. Nothing stops comparison sorting from being stable. Since

Re: Q: sort's key and cmp parameters

2009-10-07 Thread Bearophile
Paul Rubin: Bearophile: sorting, and something that's surely not bug-prone. In such situation having a 'key' argument is *better*. Such sort can be stable. Nothing stops comparison sorting from being stable.  Since the rest of your post seems premised on the opposite, I hope that clears

Re: Q: sort's key and cmp parameters

2009-10-07 Thread Hrvoje Niksic
Bearophile bearophileh...@lycos.com writes: What I meant is that a general sorting routine, even in D, is better to be first of all flexible. So I think it's better for the D built-in sort to be stable, because such extra invariant allows you to use the sort in more situations. Note that

Re: Q: sort's key and cmp parameters

2009-10-07 Thread Bearophile
Hrvoje Niksic: Note that stable sort has additional memory requirements.  In situations where you don't need stability, but do need memory-efficient in-place sorting, an unstable sort might well be preferred.  This is why libraries such as C++'s STL offer both. There are stable sorts that

Re: Q: sort's key and cmp parameters

2009-10-07 Thread Luis Zarrabeitia
On Tuesday 06 October 2009 02:40:46 pm Paul Rubin wrote: The problem is that if you allow to use the cmp, lot of programmers will use it straight away, not even bothering to know what that strange 'key' argument may be useful for. And they miss the how much handy 'key' is. Given how

Re: Q: sort's key and cmp parameters

2009-10-07 Thread Raymond Hettinger
[Hrvoje Niksic] Note that stable sort has additional memory requirements.  In situations where you don't need stability, but do need memory-efficient in-place sorting, an unstable sort might well be preferred.  This is why libraries such as C++'s STL offer both. FWIW, the additional memory

Re: Q: sort's key and cmp parameters

2009-10-06 Thread Bearophile
Raymond Hettinger: Psychologically, the thing that I find to be interesting is that beginners and intermediate users seem to take to key functions more readily than old timers.  The key function seems to be an easy thing to teach (perhaps because that's the way Excel sorts and the way SQL

Re: Q: sort's key and cmp parameters

2009-10-06 Thread Paul Rubin
Bearophile bearophileh...@lycos.com writes: The problem is that if you allow to use the cmp, lot of programmers will use it straight away, not even bothering to know what that strange 'key' argument may be useful for. And they miss the how much handy 'key' is. Given how often we hear

Re: Q: sort's key and cmp parameters

2009-10-06 Thread Raymond Hettinger
[bearophile] I love the 'key', it makes my code simpler and it's simpler to understand. I am not opposed to the idea of keeping cmp, that in some rare cases may be useful or more natural. The problem is that if you allow to use the cmp, lot of programmers will use it straight away, not even

Re: Q: sort's key and cmp parameters

2009-10-06 Thread Paul Rubin
Raymond Hettinger pyt...@rcn.com writes: Once Kernighan and Ritchie put C's qsort() into the food supply, we were doomed. It was already too late. Knuth vol 3 came out in 1973(?) and its sorting half is mostly about comparison sorting. FWIW, I think the standard library's unittest module is

Re: Q: sort's key and cmp parameters

2009-10-06 Thread Bearophile
Paul Rubin: bearophile: I am having a very hard type (despite my implementation, explanations, etc) explaining to D programmers why for a flexible general-purpose function a key function argument is often better. They in the end have added something like that in the std lib, but with a weird

Re: Q: sort's key and cmp parameters

2009-10-06 Thread Steven D'Aprano
On Tue, 06 Oct 2009 12:28:00 -0700, Raymond Hettinger wrote: [bearophile] I love the 'key', it makes my code simpler and it's simpler to understand. I am not opposed to the idea of keeping cmp, that in some rare cases may be useful or more natural. The problem is that if you allow to use

Re: Q: sort's key and cmp parameters

2009-10-05 Thread Paul Rubin
Raymond Hettinger pyt...@rcn.com writes: So, it looks like the relevant comparison values can be stored in nested lists: list_of_lists = \ [[1, [3, [], []], [7, [5, [], []], []]], [19, [23, [], []], []], ] Now you've copied all the data out of the

Re: Q: sort's key and cmp parameters

2009-10-05 Thread Raymond Hettinger
So, it looks like the relevant comparison values can be stored in nested lists: list_of_lists = \ [[1, [3, [], []], [7, [5, [], []], []]], [19, [23, [], []], []], ] Now you've copied all the data out of the original tree, which is even

Re: Q: sort's key and cmp parameters

2009-10-05 Thread Raymond Hettinger
 Say you want to change the numeric comparisons so that even numbers always sort before odd numbers, ie.     -4 -2 0 2 4 ... -999 ... -1 1 3 ... This is too easy:     s = [-2, 4, 2, -1, -3, 1, -4, 0, 3]     s.sort()     s.sort(key=lambda x: x%2)     s     [-4,

Re: Q: sort's key and cmp parameters

2009-10-05 Thread Paul Rubin
Raymond Hettinger pyt...@rcn.com writes: There were two sorts in my post and only one in yours. That's why you didn't get the same answer. Whoops, missed that. When Guido made the final call, I'm sure he was balancing a number of goals including language simplification and one-way-to-do-it.

Re: Q: sort's key and cmp parameters

2009-10-05 Thread Steven D'Aprano
On Mon, 05 Oct 2009 19:34:27 -0700, Paul Rubin wrote: Raymond Hettinger pyt...@rcn.com writes: When Guido made the final call, I'm sure he was balancing a number of goals including language simplification and one-way-to-do-it. I'm also sure that sorting lazily evaluated infinite sequences

Re: Q: sort's key and cmp parameters

2009-10-04 Thread Raymond Hettinger
[Paul Rubin] Example of list of trees (nested dicts). In practice you could get such a list from the simplejson module: list_of_trees = [{'value':1, 'left':{'value':3,'left':None,'right':None}, 'right':{'value':7,'left':{'value':5, ...}}},

Re: Q: sort's key and cmp parameters

2009-10-03 Thread Raymond Hettinger
[Paul Rubin] The idea was that you have a list of trees that you want to sort, and an ordering relation between trees:    def gt(tree1, tree2): ... where you recursively descend both trees until you find an unequal pair of nodes.  You're not trying to sort the nodes within a single tree.

Re: Q: sort's key and cmp parameters

2009-10-03 Thread Raymond Hettinger
[Paul Rubin] The idea was that you have a list of trees that you want to sort, and an ordering relation between trees:    def gt(tree1, tree2): ... Are the trees user defined classes? Can the gt() function be added incorporated as __lt__ method so that you can just run a plain sort:

Re: Q: sort's key and cmp parameters

2009-10-03 Thread Paul Rubin
Raymond Hettinger pyt...@rcn.com writes: Can you give an example of a list of trees and a cmp function that recursively compares them? Example of list of trees (nested dicts). In practice you could get such a list from the simplejson module: list_of_trees = [{'value':1,

Re: Q: sort's key and cmp parameters

2009-10-03 Thread Paul Rubin
Paul Rubin http://phr...@nospam.invalid writes: Example comparison function: def compare(tree1, tree2): c = cmp(tree1['value'], tree2['value']) if c != 0: return c c = cmp(tree1['left'], tree2['left']) if c != 0: return c return cmp(tree1['right'],

Re: Q: sort's key and cmp parameters

2009-10-03 Thread Paul Rubin
Paul Rubin http://phr...@nospam.invalid writes: c = compare(tree1['left'], tree2['left']) Of course this recursive call crashes if either branch is None. Oh well, I'll stop trying to correct it since I'm sure you get the idea. -- http://mail.python.org/mailman/listinfo/python-list

Re: Q: sort's key and cmp parameters

2009-10-03 Thread kj
In 7xtyyhikrl@ruckus.brouhaha.com Paul Rubin http://phr...@nospam.invalid writes: Python 2.x provides two ways and you can use whichever one fits the application better. I have never understood why Python 3.x finds it necessary to break one of them. Maybe I can migrate to Haskell by the

Re: Q: sort's key and cmp parameters

2009-10-03 Thread Paul Rubin
kj no.em...@please.post writes: !!! Maybe Haskell is much handier than I give it credit for, but it's hard for me to imagine that it is as convenient as Python 3, even without the cmp sort option... Heh, yeah, I was being a bit snarky/grouchy. Haskell has a very steep learning curve and

Re: Q: sort's key and cmp parameters

2009-10-02 Thread Duncan Booth
Paul Rubin http://phr...@nospam.invalid wrote: Duncan Booth duncan.bo...@invalid.invalid writes: Is there a real-life sorting task that requires (or is far more efficient with) cmp and can't be easily achieved with key and reverse? There is no sorting task that *requires* cmp. If all

Re: Q: sort's key and cmp parameters

2009-10-02 Thread Scott David Daniels
Paul Rubin wrote: I still have never understood why cmp was removed. Sure, key is more convenient a lot (or maybe most) of the time, but it's not always. Not just more convenient. cmp will always be N log N, in that _every_ comparison runs your function, while key is linear, in that it is

Re: Q: sort's key and cmp parameters

2009-10-02 Thread Paul Rubin
Scott David Daniels scott.dani...@acm.org writes: Most cases are moreeasily done with key, and it is a good idea to make the most accessible way to a sort be the most efficient one. In the rare case that you really want each comparison, the cmp-injection function will do nicely (and can be

Re: Q: sort's key and cmp parameters

2009-10-02 Thread Raymond Hettinger
[Paul Rubin] Yes, think of sorting tree structures where you have to recursively compare them til you find an unequal pair of nodes.   I'm not sure what you mean by this. What are the semantics of sorting a tree? Can you outline an example of tree that could be sorted easily with a cmp

Re: Q: sort's key and cmp parameters

2009-10-02 Thread Paul Rubin
Raymond Hettinger pyt...@rcn.com writes: I'm not sure what you mean by this. What are the semantics of sorting a tree? Can you outline an example of tree that could be sorted easily with a cmp function but not a key function? The idea was that you have a list of trees that you want to sort,

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Laszlo Nagy
Is this a homework? Challenge: to come up with a sorting task that cannot be achieved by passing to the sort method (or sorted function) suitable values for its key and reverse parameters, but instead *require* giving a value to its cmp parameter. Let me put up this question: how do you

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Peter Otten
kj wrote: Challenge: to come up with a sorting task that cannot be achieved by passing to the sort method (or sorted function) suitable values for its key and reverse parameters, but instead *require* giving a value to its cmp parameter. For example, from random import random scrambled

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Laszlo Nagy
can be achieved (to a very good approximation at least) with scrambled = some_list.sort(key=lambda x: random()) Is there a real-life sorting task that requires (or is far more efficient with) cmp and can't be easily achieved with key and reverse? The core developers don't think there

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Carsten Haese
kj wrote: Challenge: to come up with a sorting task that cannot be achieved by passing to the sort method (or sorted function) suitable values for its key and reverse parameters, but instead *require* giving a value to its cmp parameter. Such a task can't exist, because any arbitrary

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Ethan Furman
Laszlo Nagy wrote: can be achieved (to a very good approximation at least) with scrambled = some_list.sort(key=lambda x: random()) Is there a real-life sorting task that requires (or is far more efficient with) cmp and can't be easily achieved with key and reverse? The core

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Paul Rubin
kj no.em...@please.post writes: Is there a real-life sorting task that requires (or is far more efficient with) cmp and can't be easily achieved with key and reverse? Yes, think of sorting tree structures where you have to recursively compare them til you find an unequal pair of nodes. To

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Bearophile
Paul Rubin: Yes, think of sorting tree structures where you have to recursively compare them til you find an unequal pair of nodes. That's cute. In what situations do you have to perform such kind of sort? Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Duncan Booth
kj no.em...@please.post wrote: Is there a real-life sorting task that requires (or is far more efficient with) cmp and can't be easily achieved with key and reverse? There is no sorting task that *requires* cmp. If all else fails you can define a key class to wrap the original wrapper such

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Raymond Hettinger
On Oct 1, 10:08 am, kj no.em...@please.post wrote: Challenge: to come up with a sorting task that cannot be achieved by passing to the sort method (or sorted function) suitable values for its key and reverse parameters, but instead *require* giving a value to its cmp parameter. If you're

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Paul Rubin
Duncan Booth duncan.bo...@invalid.invalid writes: Is there a real-life sorting task that requires (or is far more efficient with) cmp and can't be easily achieved with key and reverse? There is no sorting task that *requires* cmp. If all else fails you can define a key class to wrap the

Re: Q: sort's key and cmp parameters

2009-10-01 Thread kj
In b8f7dea7-0fe3-4e25-9ffd-6796a6e2a...@a37g2000prf.googlegroups.com alex23 wuwe...@gmail.com writes: kj no.em...@please.post wrote: This example convinces me that it was a bad idea to get rid of cmp in Python 3, even if situations like this one are rare. It sounds like the entire point of

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Paul Rubin
Bearophile bearophileh...@lycos.com writes: Yes, think of sorting tree structures where you have to recursively compare them til you find an unequal pair of nodes. That's cute. In what situations do you have to perform such kind of sort? It came up in a search engine application I've been