6 ms·
witch is a little different thing. python can sort according to a function (using the "key" parameter) or using a custom comparator function (using the "cmp"
by _ZeD_ 13y ago
witch is a little different thing.
python can sort according to a function (using the "key" parameter) or using a custom comparator function (using the
"cmp" parameter)
as a side note, Python offers also a sorted[0] function, applyable to any sequence
[0] http://docs.python.org/2/library/functions.html#sorted http://docs.python.org/2/library/functions.html#sorted
- orblivion 13y agoI don't get the difference.
- _ZeD_ 13y agoThey resolve differenti problems: say you have a list of complex object, and you wanna sort by an attribute, you can use mylist.sort(key=lambda element: element.attribute) Elsewere, say you want to sort a non-omogeneous list, or according to a custom order (attribute1 descending, attribute2 ascending) cmp make this easy
- philh 13y agoI think ve was saying, ve doesn't see the difference between python's `sort(key=...)` and perl6's `sort { one-arg block }`. (At any rate, I don't see that difference.)
- rmc 13y agoElsewere, say you want to sort a non-omogeneous list, or according to a custom order (attribute1 descending, attribute2 ascending) cmp make this easy You can do this in python with the key function returning a list/tuple: mylist.sort(key=lambda element: (element.attribute1, -element.attribute2))
- _ZeD_ 13y agoI'm not sure you can do -"string" :)
- dded 13y agoThe Python .sort() method sorts in-place. The sorted() function returns a sorted copy, leaving the original untouched.
- orblivion 13y agoI meant I didn't understand the difference between Python's sort(key=..) and what's described here for Perl.
- _ZeD_ 13y agowhat is described for perl is python's sort(cmp=...)
- orblivion 13y agoHmm? I thought the whole point of the article was "Most people know about sort(cmp=), which takes two parameters. But check out sort(key=), it only takes one parameter, which means the key function only has to run once per item", but for Perl.
- raiph 13y agoThat's my understanding too. Lisp had a nice idiom that Randal Schwartz translated in to Perl 5 in 1994 which became known as the Schwartzian Transform: http://en.wikipedia.org/wiki/Schwartzian_Transform http://en.wikipedia.org/wiki/Schwartzian_Transform It seems from the comments here that Python's key and cmp args are Python's equivalent to the ST. I'm surprised by the comment that Python 3 removed cmp. Perhaps that's a temporary omission. This HN is about an advent article for Perl 6, which covers the same territory. Based on a look at the Rakudo code, it only implements a single custom sort closure (equivalent to Python's cmp) OR a single key closure (equivalent to Python's key).
- orblivion 13y ago> I'm surprised by the comment that Python 3 removed cmp. Perhaps that's a temporary omission. Guido and company insist that you can (almost?) always find a way supplying a key function. For better or worse, they decided it was the best way, and going by the Python philosophy they removed anything that isn't the one obvious right way to do it. Perhaps it's available in a library somehow, that's what they did to other things that are unnecessary or useful 99% of the time but may still be 1% of the time. Similarly, Guido once almost removed lambda, but he faced an insurrection.
- comex 13y agoAnd incomprehensibly, Python 3 removed the cmp argument, leaving only unwary sort.