Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #92151 > unrolled thread
| Started by | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| First post | 2015-06-06 00:07 +1000 |
| Last post | 2015-06-05 10:28 -0500 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.python
Sorting in reverse is not the same as sorting then reversing Steven D'Aprano <steve@pearwood.info> - 2015-06-06 00:07 +1000
Re: Sorting in reverse is not the same as sorting then reversing Stefan Behnel <stefan_ml@behnel.de> - 2015-06-05 16:50 +0200
Re: Sorting in reverse is not the same as sorting then reversing Peter Otten <__peter__@web.de> - 2015-06-05 16:59 +0200
Re: Sorting in reverse is not the same as sorting then reversing Skip Montanaro <skip.montanaro@gmail.com> - 2015-06-05 10:28 -0500
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2015-06-06 00:07 +1000 |
| Subject | Sorting in reverse is not the same as sorting then reversing |
| Message-ID | <5571ad0c$0$13005$c3e8da3$5496439d@news.astraweb.com> |
Sorting in reverse does not give the same result as sorting then reversing. It's easiest to see with a key function: py> a = ['fox', 'dog', 'DOG', 'cat', 'ape'] py> b = a[:] py> a.sort(key=str.lower, reverse=True) py> b.sort(key=str.lower) py> b.reverse() py> a ['fox', 'dog', 'DOG', 'cat', 'ape'] py> b ['fox', 'DOG', 'dog', 'cat', 'ape'] Sorting in reverse keeps the initial order of any equal elements unchanged. Sorting, then reversing, reverses them. (Thanks to Tim Peters for the tip.) -- Steven
[toc] | [next] | [standalone]
| From | Stefan Behnel <stefan_ml@behnel.de> |
|---|---|
| Date | 2015-06-05 16:50 +0200 |
| Message-ID | <mailman.202.1433515834.13271.python-list@python.org> |
| In reply to | #92151 |
Steven D'Aprano schrieb am 05.06.2015 um 16:07: > Sorting in reverse does not give the same result as sorting then reversing. > > It's easiest to see with a key function: > > py> a = ['fox', 'dog', 'DOG', 'cat', 'ape'] > py> b = a[:] > py> a.sort(key=str.lower, reverse=True) > py> b.sort(key=str.lower) > py> b.reverse() > py> a > ['fox', 'dog', 'DOG', 'cat', 'ape'] > py> b > ['fox', 'DOG', 'dog', 'cat', 'ape'] > > Sorting in reverse keeps the initial order of any equal elements unchanged. > Sorting, then reversing, reverses them. > > (Thanks to Tim Peters for the tip.) ... and for implementing this in the first place. :) For those of you who didn't know and now got interested, the relevant term here is "stable sorting". It means that elements that compare equal keep their relative order. That's a general property of Python's sort algorithm. All that "reverse=True" does is to change "lower than" into "greater than" and vice versa for elements that compare unequal. It does not change the behaviour for elements that compare equal, which means that they keep the same relative order in both cases (reversed/non-reversed). Stefan
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2015-06-05 16:59 +0200 |
| Message-ID | <mailman.203.1433516413.13271.python-list@python.org> |
| In reply to | #92151 |
Steven D'Aprano wrote: > Sorting in reverse does not give the same result as sorting then > reversing. > > It's easiest to see with a key function: > > > py> a = ['fox', 'dog', 'DOG', 'cat', 'ape'] > py> b = a[:] > py> a.sort(key=str.lower, reverse=True) > py> b.sort(key=str.lower) > py> b.reverse() > py> a > ['fox', 'dog', 'DOG', 'cat', 'ape'] > py> b > ['fox', 'DOG', 'dog', 'cat', 'ape'] > > > Sorting in reverse keeps the initial order of any equal elements > unchanged. Sorting, then reversing, reverses them. If there were no reverse flag you could reverse, then sort, then reverse again. > (Thanks to Tim Peters for the tip.)
[toc] | [prev] | [next] | [standalone]
| From | Skip Montanaro <skip.montanaro@gmail.com> |
|---|---|
| Date | 2015-06-05 10:28 -0500 |
| Message-ID | <mailman.204.1433518134.13271.python-list@python.org> |
| In reply to | #92151 |
On Fri, Jun 5, 2015 at 9:50 AM, Stefan Behnel <stefan_ml@behnel.de> wrote: > [Stable sorting is] a general property of Python's sort algorithm. And at times an extremely valuable property. Skip
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web