Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!nntp.club.cc.cmu.edu!micro-heart-of-gold.mit.edu!bloom-beacon.mit.edu!bloom-beacon.mit.edu!panix!roy From: Roy Smith Newsgroups: comp.lang.python Subject: Re: how to get the ordinal number in list Date: Sun, 10 Aug 2014 14:14:23 -0400 Organization: PANIX Public Access Internet and UNIX, NYC Lines: 33 Message-ID: References: <53E658CD.5020904@gmail.com> <53e59035$0$29998$c3e8da3$5496439d@news.astraweb.com> <338e8fb0-c9ec-462a-b560-1c1ff77de17e@googlegroups.com> <154cc342-7f85-4d16-b636-a1a953913c98@googlegroups.com> NNTP-Posting-Host: localhost X-Trace: reader1.panix.com 1407694464 27456 127.0.0.1 (10 Aug 2014 18:14:24 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Sun, 10 Aug 2014 18:14:24 +0000 (UTC) User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) Xref: csiph.com comp.lang.python:75995 In article <154cc342-7f85-4d16-b636-a1a953913c98@googlegroups.com>, Rustom Mody wrote: > >>> l= [6,2,9,12,1,4] > >>> sorted(l,reverse=True)[:5] > [12, 9, 6, 4, 2] > > No need to know how sorted works nor [:5] > > Now you (or Steven) can call it abstract. > > And yet its > 1. Actual running code in the interpreter > 2. Its as close as one can get to a literal translation of your > "Find the 5 largest numbers in a list" > [...] > All the above are clearer than loops+assignments and can be > taught before them I disagree. For a beginner, you want to be able to break things down into individual steps and examine the result at each point. If you do: > >>> l= [6,2,9,12,1,4] > >>> l2 = sorted(l,reverse=True) > >>> l2[:5] you have the advantage that you can stop after creating l2 and print it out. The student can see that it has indeed been sorted. With the chained operations, you have to build a mental image of an anonymous, temporary list, and then perform the slicing operation on that. Sure, it's the way you or I would write it in production code, but for a beginner, breaking it down into smaller pieces makes it easier to understand.