a query on sorting

Steve Holden steve at holdenweb.com
Thu Sep 28 09:18:13 CEST 2006


Paul McGuire wrote:
> "Steve Holden" <steve at holdenweb.com> wrote in message 
> news:mailman.796.1159357863.10491.python-list at python.org...
> <snip>
> 
>   >>> [x for x in enumerate(a)]
> [(0, 9), (1, 4), (2, 3), (3, 5), (4, 2), (5, 6), (6, 7), (7, 1), (8, 2)]
> 
> 
> Just curious, Steve, but why do this list comprehension when:
> 
> list(enumerate(a))
> 
> works just as well?
> 
Dumb stupidity would account for that. Knowing that enumerate(a) was an 
iterator I simply used the first iterative context that came to mind. 
List is somewhat more effective.

> In the interests of beating a dead horse into the ground (metaphor-mixing?), 
> I looked at using map to one-line the OP's request, and found an interesting 
> inconsistency.
> 
> I tried using map(reversed, list(enumerate(a))), but got back a list of 
> iterators.  To create the list of tuples, I have to call the tuple 
> constructor on each one, as in:
> 
> map(tuple,map(reversed,list(enumerate(a))))
> 
> However, sorted returns a list, not a listsortediterator.  Why the 
> difference in these two builtins?
> 
Ask the developers. Beats me ...

> I guess you can beat a dead horse into the ground, but you can't make him 
> drink.
> 
I guess. Thanks for flogging this horse so effectively.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list