Re: [Tutor] Sorting a list in ascending order [RESOLVED]

2016-04-30 Thread Steven D'Aprano
On Fri, Apr 29, 2016 at 07:05:27PM -0400, Ken G. wrote: > Martin: I have been using Python2 for several years now and I have yet > been able to change over to Python3. I am not together sure if Python3 > is now more stable to use and more commonly use. If so, I will gradually > change over but

Re: [Tutor] Sorting a list in ascending order [RESOLVED]

2016-04-29 Thread meenu ravi
That's actually two single quotes:-) both single and double quotes should work. Thanks, Meena On Apr 29, 2016 5:58 PM, "Ken G." wrote: > > On Fri, Apr 29, 2016 at 3:01 PM, Ken G. wrote: > >> In entering five random number, how can I best sort >> it into ascending order, such as 0511414453? Usin

Re: [Tutor] Sorting a list in ascending order [RESOLVED]

2016-04-29 Thread Ken G.
On 04/29/2016 07:40 PM, Alan Gauld via Tutor wrote: On 29/04/16 23:58, Ken G. wrote: print ''.join(list1) #making it again as a single string Thanks, Meena, that is great! I changed your last line to: print "".join(list1) and it came out as below: 0511414453 Another quotation

Re: [Tutor] Sorting a list in ascending order [RESOLVED]

2016-04-29 Thread Alan Gauld via Tutor
On 30/04/16 00:05, Ken G. wrote: > been able to change over to Python3. I am not together sure if Python3 > is now more stable to use and more commonly use. It is definitely stable and most libraries are now converted (although a few remain v2 only). Availability of libraries is now the only re

Re: [Tutor] Sorting a list in ascending order [RESOLVED]

2016-04-29 Thread Alan Gauld via Tutor
On 29/04/16 23:58, Ken G. wrote: >> print ''.join(list1) #making it again as a single string >> > > Thanks, Meena, that is great! I changed your last line to: > > print "".join(list1) and it came out as below: > > 0511414453 > > Another quotation mark was needed. No it wasn't. Meen

Re: [Tutor] Sorting a list in ascending order

2016-04-29 Thread isaac tetteh
You can use sorted(line) but you result will be in a list if thats okay. Otherwise you can iterate the list. Sent from my iPhone > On Apr 29, 2016, at 3:03 PM, Ken G. wrote: > > In entering five random number, how can I best sort > it into ascending order, such as 0511414453? Using > Linux 2.

Re: [Tutor] Sorting a list in ascending order [RESOLVED]

2016-04-29 Thread Ken G.
On 04/29/2016 05:10 PM, Martin A. Brown wrote: Greetings Ken and welcome to Python, Using Linux 2.7.6 in Ubuntu 14.04.4. Thanks. Thank you for this information. I have one tip for you: While Python 2.x will still be around for a while, if you are learning Python today, I'd suggest Python 3

Re: [Tutor] Sorting a list in ascending order [RESOLVED]

2016-04-29 Thread Ken G.
On Fri, Apr 29, 2016 at 3:01 PM, Ken G. > wrote: In entering five random number, how can I best sort it into ascending order, such as 0511414453? Using Linux 2.7.6 in Ubuntu 14.04.4. Thanks. number01 = "41" number02 = "11" number03 = "05"

Re: [Tutor] Sorting a list in ascending order

2016-04-29 Thread Martin A. Brown
Greetings Ken and welcome to Python, > Using Linux 2.7.6 in Ubuntu 14.04.4. Thanks. Thank you for this information. I have one tip for you: While Python 2.x will still be around for a while, if you are learning Python today, I'd suggest Python 3.x. You can read more about the differences on

Re: [Tutor] Sorting a list in ascending order

2016-04-29 Thread meenu ravi
Hi Ken, As the message clearly says, the string object doesn't have an attribute "sort". You are trying to join the inputs as a single string,"line" and then you are trying to sort it. But, as you want the string in the sorted format, you should sort it first and then convert it into string, as fo

[Tutor] Sorting a list in ascending order

2016-04-29 Thread Ken G.
In entering five random number, how can I best sort it into ascending order, such as 0511414453? Using Linux 2.7.6 in Ubuntu 14.04.4. Thanks. number01 = "41" number02 = "11" number03 = "05" number04 = "53" number05 = "44" line = number01 + number02 + number03 + number04 + number05 print print lin

Re: [Tutor] Sorting a list of list

2015-06-05 Thread Laura Creighton
In a message of Fri, 05 Jun 2015 21:16:33 +0100, Stephen Nelson-Smith writes: >As part of my league secretary program (to which thread I shall reply again >shortly), I need to sort a list of lists. I've worked out that I can use >sorted() and operator.itemgetter to sort by a value at a known posit

Re: [Tutor] Sorting a list of list

2015-06-05 Thread Mark Lawrence
On 05/06/2015 21:16, Stephen Nelson-Smith wrote: As part of my league secretary program (to which thread I shall reply again shortly), I need to sort a list of lists. I've worked out that I can use sorted() and operator.itemgetter to sort by a value at a known position in each list. Is it possi

Re: [Tutor] Sorting a list of list

2015-06-05 Thread Peter Otten
Stephen Nelson-Smith wrote: > As part of my league secretary program (to which thread I shall reply > again > shortly), I need to sort a list of lists. I've worked out that I can use > sorted() and operator.itemgetter to sort by a value at a known position in > each list. Is it possible to do th

[Tutor] Sorting a list of list

2015-06-05 Thread Stephen Nelson-Smith
As part of my league secretary program (to which thread I shall reply again shortly), I need to sort a list of lists. I've worked out that I can use sorted() and operator.itemgetter to sort by a value at a known position in each list. Is it possible to do this at a secondary level? So if the ite

Re: [Tutor] Sorting a List

2011-01-12 Thread Peter Otten
Bill Campbell wrote: > On Wed, Jan 12, 2011, Corey Richardson wrote: >>Hello Tutors, >> >>I am generating XML definitions for animations to be used in a >>FIFE-based game. I need to sort the frames of the animations, and I am >>currently using: >>sorted([image for image in os.listdir(path) if imag

Re: [Tutor] Sorting a List

2011-01-12 Thread Corey Richardson
On 01/12/2011 09:42 PM, Luke Paireepinart wrote: > Remember the sorted() method takes a key function, have this key > function take in each filename and compare the numbers and you're all set! > > - > Sent from a mobile device. Apologies for brevity and top-posting. > -

Re: [Tutor] Sorting a List

2011-01-12 Thread Luke Paireepinart
Remember the sorted() method takes a key function, have this key function take in each filename and compare the numbers and you're all set! - Sent from a mobile device. Apologies for brevity and top-posting. - On Jan 12, 2011, at 8:17 PM, B

Re: [Tutor] Sorting a List

2011-01-12 Thread Bill Allen
Corey, I have a bit of code that use in a CGI that sorts some picture files, perhaps something like this will work for you. The file sorting bit: dir_list = os.listdir("/localhost/html/pics") dir_list.sort() #sorts the list of filenames in place Just in case there is something else useful in th

Re: [Tutor] Sorting a List

2011-01-12 Thread Bill Campbell
On Wed, Jan 12, 2011, Corey Richardson wrote: >Hello Tutors, > >I am generating XML definitions for animations to be used in a >FIFE-based game. I need to sort the frames of the animations, and I am >currently using: >sorted([image for image in os.listdir(path) if image.endswith('.png')]) > >The fi

Re: [Tutor] Sorting a List

2011-01-12 Thread Emile van Sebille
On 1/12/2011 4:56 PM Corey Richardson said... Hello Tutors, I am generating XML definitions for animations to be used in a FIFE-based game. I need to sort the frames of the animations, and I am currently using: sorted([image for image in os.listdir(path) if image.endswith('.png')]) I might use

[Tutor] Sorting a List

2011-01-12 Thread Corey Richardson
Hello Tutors, I am generating XML definitions for animations to be used in a FIFE-based game. I need to sort the frames of the animations, and I am currently using: sorted([image for image in os.listdir(path) if image.endswith('.png')]) The final output in the XML is:

Re: [Tutor] Sorting a list

2009-05-14 Thread Walker Hale IV
The prize goes to Bob Gailer! This solution is not only shortest, but works perfectly with negative numbers. -- Walker Hale On Thu, May 14, 2009 at 1:22 PM, bob gailer wrote: > Lie Ryan wrote: >> >> Timo wrote: >>> >>> Hello, >>> >>> I don't think this should be difficult, so maybe I look over

Re: [Tutor] Sorting a list

2009-05-14 Thread bob gailer
Lie Ryan wrote: Timo wrote: Hello, I don't think this should be difficult, so maybe I look over it. But I can't seem to find the solution. I have a list with one word and a couple of numbers. Now I want the word to be kept in the first location and the numbers to be sorted. So this: [4, 6

Re: [Tutor] Sorting a list

2009-05-14 Thread bob gailer
Christian Witts wrote: Kent Johnson wrote: On Thu, May 14, 2009 at 3:34 AM, Timo wrote: Emile van Sebille schreef: If this is always the case you can use the sort method of lists, then relocate the last entry to the front -- a = [4, 6, 'word', 3, 9] a.sort() a.insert(0,a

Re: [Tutor] Sorting a list

2009-05-14 Thread Kent Johnson
On Thu, May 14, 2009 at 9:13 AM, Lie Ryan wrote: > Why not: sorted(lst, key=lambda x: (0, x) if isinstance(x, str) else (1, x)) > ['word', 3, 4, 6, 9] > > I think sorting tuple has a well-defined meaning, i.e. sort on first item, > then second, then third, etc... and there is AFAIK no restric

Re: [Tutor] Sorting a list

2009-05-14 Thread Kent Johnson
On Wed, May 13, 2009 at 1:25 PM, Alan Gauld wrote: > def compare(x,y): >    if type(x) == str:  return  -1 >    if type(y) == str: return 1 >    return cmp(x,y) > > > Runs into problems if you have multiple strings and want them sorted too... A slight variation - if the types are the same, use c

Re: [Tutor] Sorting a list

2009-05-14 Thread Lie Ryan
Timo wrote: Hello, I don't think this should be difficult, so maybe I look over it. But I can't seem to find the solution. I have a list with one word and a couple of numbers. Now I want the word to be kept in the first location and the numbers to be sorted. So this: [4, 6, 'word', 3, 9]

Re: [Tutor] Sorting a list

2009-05-14 Thread spir
Le Thu, 14 May 2009 12:24:15 +0200, Christian Witts s'exprima ainsi: > The ability to compare unlike objects at all is considered a > mis-feature and has been removed in Python 3: > http://mail.python.org/pipermail/python-dev/2004-June/045111.html Great! Thanks for the informaton, Kent. Denis -

Re: [Tutor] Sorting a list

2009-05-14 Thread Christian Witts
Kent Johnson wrote: On Thu, May 14, 2009 at 3:34 AM, Timo wrote: Emile van Sebille schreef: If this is always the case you can use the sort method of lists, then relocate the last entry to the front -- a = [4, 6, 'word', 3, 9] a.sort() a.insert(0,a.pop()) a

Re: [Tutor] Sorting a list

2009-05-14 Thread Kent Johnson
On Thu, May 14, 2009 at 3:34 AM, Timo wrote: > Emile van Sebille schreef: >> If this is always the case you can use the sort method of lists, then >> relocate the last entry to the front -- >> >> >>> a = [4, 6, 'word', 3, 9] >> >>> a.sort() >> >>> a.insert(0,a.pop()) >> >>> a >> ['word', 3, 4, 6,

Re: [Tutor] Sorting a list

2009-05-14 Thread Timo
Emile van Sebille schreef: On 5/13/2009 9:25 AM Timo said... Hello, I don't think this should be difficult, so maybe I look over it. But I can't seem to find the solution. I have a list with one word and a couple of numbers. If this is always the case you can use the sort method of lists,

Re: [Tutor] Sorting a list

2009-05-13 Thread spir
Le Wed, 13 May 2009 23:12:29 +0530, Ataulla S H s'exprima ainsi: > >>> k = [each for each in l if type(each) == type(int())] You can write "int" (is a type since py 2.2, I guess). assert int==type(int()) # ok Denis -- la vita e estrany ___

Re: [Tutor] Sorting a list

2009-05-13 Thread Ataulla S H
>>> l = [4, 6, 'word','a', 3, 9] >>> l.sort() >>> l [3, 4, 6, 9, 'a', 'word'] >>> k = [each for each in l if type(each) == type(int())] >>> k [3, 4, 6, 9] >>> kex = [each for each in l if type(each) != type(int())] >>> kex ['a', 'word'] >>> kex+k ['a', 'word', 3, 4, 6, 9] On Wed, May 13, 2009 at

Re: [Tutor] Sorting a list

2009-05-13 Thread Alan Gauld
"vince spicer" wrote def compare(x,y): try: return cmp(int(x), int(y)) except: pass try: int(x) except: return -1 try: int(y) except: return 1 return 0 Or def compare(x,y): if type(x) == str: return -1 if type(y) == s

Re: [Tutor] Sorting a list

2009-05-13 Thread vince spicer
you can pass sort a custom compare function Simple ex: def compare(x,y): try: return cmp(int(x), int(y)) except: pass try: int(x) except: return -1 try: int(y) except: return 1 return 0 x = [4, 6, 'word', 3, 9] x.sort(

Re: [Tutor] Sorting a list

2009-05-13 Thread Emile van Sebille
On 5/13/2009 9:25 AM Timo said... Hello, I don't think this should be difficult, so maybe I look over it. But I can't seem to find the solution. I have a list with one word and a couple of numbers. If this is always the case you can use the sort method of lists, then relocate the last entr

[Tutor] Sorting a list

2009-05-13 Thread Timo
Hello, I don't think this should be difficult, so maybe I look over it. But I can't seem to find the solution. I have a list with one word and a couple of numbers. Now I want the word to be kept in the first location and the numbers to be sorted. So this: [4, 6, 'word', 3, 9] should be: [

Re: [Tutor] Sorting a list in an add order

2006-09-06 Thread Alan Gauld
Matt, > However, I want to process them so that they are arranged in the > correct order, which means I need to sort the list of files. Of > course, because they aren't named in any (obvious) order, I'm a bit > stuck. > > I thought about using a dictionary to map names and order: so > {"OAF":1

Re: [Tutor] Sorting a list in an add order

2006-09-06 Thread Kent Johnson
Michael Janssen wrote: > you can specify your own comparing funtion for aList.sort(). Since Python 2.4, supplying a key function to list.sort() is generally preferable to a cmp function - the key function is easier to write and it is more efficient to use. See my reply to Matt for an example. K

Re: [Tutor] Sorting a list in an add order

2006-09-06 Thread Kent Johnson
Matt Williams wrote: > Dear List, > > I've written a small script to extract the definitions from my thesis, > and output them as a .tex file, which works ok but I have a small problem. > > The input is done by specifying a directory, and using glob to find the > ".tex" filenames. > > However, I

Re: [Tutor] Sorting a list in an add order

2006-09-06 Thread Michael Janssen
On 9/6/06, Matt Williams <[EMAIL PROTECTED]> wrote: > The input is done by specifying a directory, and using glob to find the > ".tex" filenames. > > However, I want to process them so that they are arranged in the correct > order, which means I need to sort the list of files. Of course, because >

[Tutor] Sorting a list in an add order

2006-09-06 Thread Matt Williams
Dear List, I've written a small script to extract the definitions from my thesis, and output them as a .tex file, which works ok but I have a small problem. The input is done by specifying a directory, and using glob to find the ".tex" filenames. However, I want to process them so that they ar

Re: [Tutor] Sorting a list of objects on different fields

2006-03-02 Thread Terry Carroll
On Thu, 2 Mar 2006, Terry Carroll wrote: > Thanks, Kent! I didn't know about "key=". I see it's new in 2.4. > I was thinking I'd have to put in a method for each potentially sortable > field. And, in the spirit of RTFM, I should have been looking here first: http://wiki.python.org/moin/How

Re: [Tutor] Sorting a list of objects on different fields

2006-03-02 Thread Terry Carroll
On Thu, 2 Mar 2006, Kent Johnson wrote: > In [9]: lst.sort(key=attrgetter('b')); print lst Thanks, Kent! I didn't know about "key=". I see it's new in 2.4. I was thinking I'd have to put in a method for each potentially sortable field. ___ Tutor ma

Re: [Tutor] Sorting a list of objects on different fields

2006-03-02 Thread Kent Johnson
Terry Carroll wrote: > I have a list (or a list-like object, doesn't matter) of objects, each of > which has multiple fields. I want to be able to arbitrarily sort the list > on any of them, or at least on several of them. > > To make this a little more concrete, here's a simplified idea. Say th

[Tutor] Sorting a list of objects on different fields

2006-03-02 Thread Terry Carroll
I have a list (or a list-like object, doesn't matter) of objects, each of which has multiple fields. I want to be able to arbitrarily sort the list on any of them, or at least on several of them. To make this a little more concrete, here's a simplified idea. Say the objects each represent a fil

Re: [Tutor] Sorting a list of lists aka nested lists

2005-08-19 Thread Kent Johnson
mailing list wrote: > No-one's mentioned a Schwartzian Transform yet ; ) because it's obsoleted by the key parameter to sort... > > > > On 8/15/05, Kent Johnson <[EMAIL PROTECTED]> wrote: > >>[EMAIL PROTECTED] wrote: >> >>>Note that in Python2.4+, you can use key= instead: >>> >>>def sortKey

Re: [Tutor] Sorting a list of lists aka nested lists

2005-08-19 Thread mailing list
No-one's mentioned a Schwartzian Transform yet ; ) On 8/15/05, Kent Johnson <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > Note that in Python2.4+, you can use key= instead: > > > > def sortKey(lst): > > return lst[2] > > Quant.sort(key=sortKey) > > > > This is more effient than spe

Re: [Tutor] Sorting a list of lists aka nested lists

2005-08-15 Thread Kent Johnson
[EMAIL PROTECTED] wrote: > Note that in Python2.4+, you can use key= instead: > > def sortKey(lst): > return lst[2] > Quant.sort(key=sortKey) > > This is more effient than specifying a different comparison function, because > the key function is only called once for each element. And instead of

Re: [Tutor] Sorting a list of lists aka nested lists

2005-08-14 Thread jfouhy
Quoting Alan G <[EMAIL PROTECTED]>: > > Quant.append( [ db_ticker, stock_close, MTD, 0, QTD, 0, YTD, 0, > > 0, 0 ] ) > > After Quant is created, I want to sort it by MTD. If I use a simple > > Quant.sort(), I assume its going to sort by 'db_ticker' which is not > > what I want. > you need to wr

Re: [Tutor] Sorting a list of lists aka nested lists

2005-08-14 Thread Alan G
>Quant.append( [ db_ticker, stock_close, MTD, 0, QTD, 0, YTD, 0, > 0, 0 ] ) > > After Quant is created, I want to sort it by MTD. If I use a simple > Quant.sort(), I assume its going to sort by 'db_ticker' which is not > what I want. you need to write your own comparison function. Basicall

Re: [Tutor] Sorting a list of lists aka nested lists

2005-08-13 Thread Danny Yoo
> >AMK has written a nice mini-tutorial on how to use a list's sort() > >effectively: > > > >http://www.amk.ca/python/howto/sorting/sorting.html > > > >Does his tutorial make sense, or are there parts in there that are > >baffling? Please feel free to ask questions on it, and we'll try to >

Re: [Tutor] Sorting a list of lists aka nested lists

2005-08-13 Thread Jim Roush
Danny Yoo wrote: >On Sat, 13 Aug 2005, Jim Roush wrote: > > > >>I have a python script that creates a list of lists like so: >> >>Quant.append( [ db_ticker, stock_close, MTD, 0, QTD, 0, YTD, 0, 0, 0 ] ) >> >>After Quant is created, I want to sort it by MTD. If I use a simple >>Quant.sort(),

Re: [Tutor] Sorting a list of lists aka nested lists

2005-08-13 Thread Danny Yoo
On Sat, 13 Aug 2005, Jim Roush wrote: > I have a python script that creates a list of lists like so: > > Quant.append( [ db_ticker, stock_close, MTD, 0, QTD, 0, YTD, 0, 0, 0 ] ) > > After Quant is created, I want to sort it by MTD. If I use a simple > Quant.sort(), I assume its going to sor

[Tutor] Sorting a list of lists aka nested lists

2005-08-13 Thread Jim Roush
I have a python script that creates a list of lists like so: Quant.append( [ db_ticker, stock_close, MTD, 0, QTD, 0, YTD, 0, 0, 0 ] ) After Quant is created, I want to sort it by MTD. If I use a simple Quant.sort(), I assume its going to sort by 'db_ticker' which is not what I want. I've

Re: [Tutor] sorting a list of dictionaries

2005-04-15 Thread Kent Johnson
PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Kent Johnson Sent: Wednesday, April 13, 2005 3:52 AM Cc: tutor@python.org Subject: Re: [Tutor] sorting a list of dictionaries -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Thursday, December 09, 2004 12:19 PM

RE: [Tutor] sorting a list of dictionaries

2005-04-15 Thread Gooch, John
Satellite L.L.C. 9601 S. Meridian Blvd. Englewood, CO 80112 Desk: 720-514-5708 -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Kent Johnson Sent: Wednesday, April 13, 2005 3:52 AM Cc: tutor@python.org Subject: Re: [Tutor] sorting a list of di

Re: [Tutor] sorting a list of dictionaries

2005-04-13 Thread Kent Johnson
-Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Thursday, December 09, 2004 12:19 PM To: tutor@python.org Subject: Re: [Tutor] sorting a list of dictionaries On 9 Dez 2004, [EMAIL PROTECTED] wrote: I have a list of dictionaries, each representing info about a

Re: [Tutor] sorting a list of dictionaries

2005-04-12 Thread Sean Perry
Gooch, John wrote: I am working on a dictionary sorting problem just like the one in the email thread at the bottom of this message. My question about their solution is: In these lines: lst.sort(lambda m, n: cmp(m.get(field), n.get(field))) where field is either 'name' or 'size'. Wh

RE: [Tutor] sorting a list of dictionaries

2005-04-12 Thread Gooch, John
;. What is "n:" and what is "lambda m" ? Thank You, John A. Gooch -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Thursday, December 09, 2004 12:19 PM To: tutor@python.org Subject: Re: [Tutor] sorting a list of dictionaries On 9 Dez 20

Re: [Tutor] sorting a list of dictionaries

2004-12-09 Thread Larry Holish
On Thu, Dec 09, 2004 at 03:22:29PM -0500, Kent Johnson wrote: > Using sort() with a user compare function is not recommended when you > care about performance. The problem is that the sort function has to > call back into Python code for every compare, of which there are many. > The decorate - sort

Re: [Tutor] sorting a list of dictionaries

2004-12-09 Thread Kent Johnson
Using sort() with a user compare function is not recommended when you care about performance. The problem is that the sort function has to call back into Python code for every compare, of which there are many. The decorate - sort - undecorate idiom is the preferred way to do this in Python < 2.4

Re: [Tutor] sorting a list of dictionaries

2004-12-09 Thread Karl Pflästerer
On 9 Dez 2004, [EMAIL PROTECTED] wrote: > I have a list of dictionaries, each representing info about a file, > something like: > > [{'name':'foo.txt','size':35}, {'name':'bar.txt','size':35}, ...] > > I want to present a sorted list of all the files' data, sorting on the > keys 'name' or 'size'.

Re: [Tutor] sorting a list of dictionaries

2004-12-09 Thread Kent Johnson
If you can use Python 2.4 it is very simple using the new key= parameter to sort and operator.itemgetter: >>> import operator >>> ds = [{'name':'foo.txt','size':35}, {'name':'bar.txt','size':36}] >>> ds.sort(key=operator.itemgetter('name')) >>> ds [{'name': 'bar.txt', 'size': 36}, {'name': 'foo.t

[Tutor] sorting a list of dictionaries

2004-12-09 Thread Larry Holish
Hello, I have a list of dictionaries, each representing info about a file, something like: [{'name':'foo.txt','size':35}, {'name':'bar.txt','size':35}, ...] I want to present a sorted list of all the files' data, sorting on the keys 'name' or 'size'. The file 'name' s should be unique (I'm hopin