Re: sorting a list of dicts by a computed field

2017-01-31 Thread jladasky
On Tuesday, January 31, 2017 at 12:34:38 PM UTC-8, MRAB wrote: > On 2017-01-31 20:26, Larry Martell wrote: > The module 'datetime' contains a class called 'datetime'. Judging by > your exception, 'datetime' is the module. > > Try "datetime.datetime.strptime" instead. This re-use of the name "da

Re: sorting a list of dicts by a computed field

2017-01-31 Thread Larry Martell
On Tue, Jan 31, 2017 at 3:30 PM, Chris Angelico wrote: > On Wed, Feb 1, 2017 at 7:26 AM, Larry Martell wrote: >> I have a list of dicts and one item of the dict is a date in m/d/Y >> format. I want to sort by that. I tried this: >> >> sorted(data['trends'], key=lambda k: >> datetime.strptime(k['d

Re: sorting a list of dicts by a computed field

2017-01-31 Thread Michael Torrie
On 01/31/2017 01:26 PM, Larry Martell wrote: > I have a list of dicts and one item of the dict is a date in m/d/Y > format. I want to sort by that. I tried this: > > sorted(data['trends'], key=lambda k: > datetime.strptime(k['date_time'],'%m/%d/%Y')) > > But that fails with: > > Exception Type:

Re: sorting a list of dicts by a computed field

2017-01-31 Thread MRAB
On 2017-01-31 20:26, Larry Martell wrote: I have a list of dicts and one item of the dict is a date in m/d/Y format. I want to sort by that. I tried this: sorted(data['trends'], key=lambda k: datetime.strptime(k['date_time'],'%m/%d/%Y')) But that fails with: Exception Type: AttributeError at /

Re: sorting a list of dicts by a computed field

2017-01-31 Thread Kev Dwyer
Larry Martell wrote: > I have a list of dicts and one item of the dict is a date in m/d/Y > format. I want to sort by that. I tried this: > > sorted(data['trends'], key=lambda k: > datetime.strptime(k['date_time'],'%m/%d/%Y')) > > But that fails with: > > Exception Type: AttributeError at > /re

Re: sorting a list of dicts by a computed field

2017-01-31 Thread Chris Angelico
On Wed, Feb 1, 2017 at 7:26 AM, Larry Martell wrote: > I have a list of dicts and one item of the dict is a date in m/d/Y > format. I want to sort by that. I tried this: > > sorted(data['trends'], key=lambda k: > datetime.strptime(k['date_time'],'%m/%d/%Y')) > > But that fails with: > > Exception

Re: Sorting a list

2016-04-04 Thread Peter Otten
Random832 wrote: > On Mon, Apr 4, 2016, at 02:56, Peter Otten wrote: >> > That works well. Why is it 'cheating'? >> >> On second thought it isn't ;) > > It does require a numeric type, though. There are lots of types that are > orderable but do not have a negation operator that provides a key w

Re: Sorting a list

2016-04-04 Thread Random832
On Mon, Apr 4, 2016, at 02:56, Peter Otten wrote: > > That works well. Why is it 'cheating'? > > On second thought it isn't ;) It does require a numeric type, though. There are lots of types that are orderable but do not have a negation operator that provides a key with reversed ordering. -- ht

Re: Sorting a list

2016-04-03 Thread Peter Otten
DFS wrote: > On 4/3/2016 3:31 PM, Peter Otten wrote: > from operator import itemgetter as get > print "\n".join("{1} {0}".format(*p) for p in sorted( >> ... sorted(colCounts, key=get(1)), key=get(0), reverse=True)) > > Kind of clunky looking. Is that why don't you recommend it? That, a

Re: Sorting a list

2016-04-03 Thread DFS
On 4/3/2016 3:31 PM, Peter Otten wrote: DFS wrote: cntText = 60 cntBool = 20 cntNbrs = 30 cntDate = 20 cntBins = 20 strText = " text: " strBool = " boolean: " strNbrs = " numeric: " strDate = " date-time:" strBins = " binary: " colCounts = [(cntText,strText) , (cntBool,strBool),

Re: Sorting a list

2016-04-03 Thread Peter Otten
DFS wrote: > cntText = 60 > cntBool = 20 > cntNbrs = 30 > cntDate = 20 > cntBins = 20 > > strText = " text: " > strBool = " boolean: " > strNbrs = " numeric: " > strDate = " date-time:" > strBins = " binary: " > > colCounts = [(cntText,strText) , (cntBool,strBool), (cntNbrs,strNbrs)

Re: Sorting a list

2016-04-03 Thread DFS
On 4/3/2016 2:30 PM, DFS wrote: cntText = 60 cntBool = 20 cntNbrs = 30 cntDate = 20 cntBins = 20 strText = " text: " strBool = " boolean: " strNbrs = " numeric: " strDate = " date-time:" strBins = " binary: " colCounts = [(cntText,strText) , (cntBool,strBool), (cntNbrs,strNbrs) , (

Re: Sorting a list created from a parsed xml message

2010-07-27 Thread kak...@gmail.com
On Jul 22, 12:56 pm, Thomas Jollans wrote: > On 07/21/2010 03:38 PM, kak...@gmail.com wrote: > > > > > On Jul 21, 9:04 am, "kak...@gmail.com" wrote: > >> On Jul 21, 8:58 am, Stefan Behnel wrote: > > >>> kak...@gmail.com, 21.07.2010 14:36: > > From the subject of my message it's clear that i

Re: Sorting a list created from a parsed xml message

2010-07-22 Thread Thomas Jollans
On 07/21/2010 03:38 PM, kak...@gmail.com wrote: > On Jul 21, 9:04 am, "kak...@gmail.com" wrote: >> On Jul 21, 8:58 am, Stefan Behnel wrote: >> >> >> >>> kak...@gmail.com, 21.07.2010 14:36: >> From the subject of my message it's clear that i get an xml message from a socket, >> >>> Not a

Re: Sorting a list created from a parsed xml message

2010-07-21 Thread Stefan Behnel
kak...@gmail.com, 21.07.2010 15:38: On Jul 21, 9:04 am, "kak...@gmail.com" wrote: On Jul 21, 8:58 am, Stefan Behnel wrote: kak...@gmail.com, 21.07.2010 14:36: From the subject of my message it's clear that i get an xml message from a socket, Not at all, but now that you say it...

Re: Sorting a list created from a parsed xml message

2010-07-21 Thread kak...@gmail.com
On Jul 21, 9:04 am, "kak...@gmail.com" wrote: > On Jul 21, 8:58 am, Stefan Behnel wrote: > > > > > kak...@gmail.com, 21.07.2010 14:36: > > > > From the subject of my message it's clear that i get an xml message > > > from a socket, > > > Not at all, but now that you say it... > > > > i parse it a

Re: Sorting a list created from a parsed xml message

2010-07-21 Thread kak...@gmail.com
On Jul 21, 8:58 am, Stefan Behnel wrote: > kak...@gmail.com, 21.07.2010 14:36: > > > From the subject of my message it's clear that i get an xml message > > from a socket, > > Not at all, but now that you say it... > > > > > i parse it and the result is a list like the one that > > follows: > > ID

Re: Sorting a list created from a parsed xml message

2010-07-21 Thread Stefan Behnel
kak...@gmail.com, 21.07.2010 14:36: From the subject of my message it's clear that i get an xml message from a socket, Not at all, but now that you say it... i parse it and the result is a list like the one that follows: ID_Col 4 Serverak ip OFFLINE 29

Re: Sorting a list of lists

2010-02-13 Thread R (Chandra) Chandrasekhar
MRAB wrote: You'd have to post an example of that, but you could try deleting some of the entries before sorting so see whether you can still reproduce the problem with a smaller list. John Posner wrote: Please cut-and-paste the exact error message (or other evidence of "failure") into a mess

Re: Sorting a list of lists

2010-02-12 Thread John Posner
On 2/12/2010 12:45 PM, R (Chandra) Chandrasekhar wrote: Dear Folks, I have lines of values like so: 14, [25, 105, 104] 10, [107, 106, 162] 21, [26, 116, 165] I need to sort them in two ways: (a) By the numeric value of the first column; and (b) by the sum of the elements of the second item i

Re: Sorting a list of lists

2010-02-12 Thread MRAB
R (Chandra) Chandrasekhar wrote: Dear Folks, I have lines of values like so: 14, [25, 105, 104] 10, [107, 106, 162] 21, [26, 116, 165] I need to sort them in two ways: (a) By the numeric value of the first column; and (b) by the sum of the elements of the second item in each list, which is

Re: Sorting a list with entries of unequal types

2010-01-29 Thread Peter Otten
Ben Finney wrote: > Peter Otten <__pete...@web.de> writes: > >> I can't find the relevant part of the 2.6 documentation, but something >> like >> >> >>> def key(x): >> ... t = type(x) >> ... t = compat.get(t, t) >> ... return t.__name__, id(t), x >> ... >> >>> compat = {bool: float, i

Re: Sorting a list with entries of unequal types

2010-01-28 Thread Ben Finney
Terry Reedy writes: > And complex numbers, and probably some other things, cannot be sorted > even in Python2. Universal sorting is a broken idea, so Python3 leaves > it to you to say what *you* mean. Okay. I guess I'd better figure out what that is, for this code base, before porting it to Pyth

Re: Sorting a list with entries of unequal types

2010-01-28 Thread Terry Reedy
On 1/28/2010 8:24 PM, Steven D'Aprano wrote: Um, what ordering do you want? The same ordering I'd get in Python 2; that is, determined by the types of the elements. The ordering has not been consistent across minor versions of Python 2: And complex numbers, and probably some other things,

Re: Sorting a list with entries of unequal types

2010-01-28 Thread Steven D'Aprano
On Fri, 29 Jan 2010 12:13:50 +1100, Ben Finney wrote: > Paul Rubin writes: > >> Ben Finney writes: >> > So how should I be sorting a list with entries of “unequal types” >> > such that it will work in Python 3? >> >> Um, what ordering do you want? > > The same ordering I'd get in Python 2; tha

Re: Sorting a list with entries of unequal types

2010-01-28 Thread Ben Finney
Paul Rubin writes: > Ben Finney writes: > > So how should I be sorting a list with entries of “unequal types” > > such that it will work in Python 3? > > Um, what ordering do you want? The same ordering I'd get in Python 2; that is, determined by the types of the elements. Peter Otten <__pete

Re: Sorting a list with entries of unequal types

2010-01-28 Thread Steven D'Aprano
On Fri, 29 Jan 2010 11:01:21 +1100, Ben Finney wrote: > So how should I be sorting a list with entries of “unequal types” such > that it will work in Python 3? That depends on how you want the items to be sorted. Python 2.x sorted unequal types in some arbitrary but consistent order. If that's a

Re: Sorting a list with entries of unequal types

2010-01-28 Thread MRAB
Paul Rubin wrote: Ben Finney writes: So how should I be sorting a list with entries of “unequal types” such that it will work in Python 3? Um, what ordering do you want? Basically you have to write a custom key function (they removed the option of writing a comparison function). Maybe somet

Re: Sorting a list with entries of unequal types

2010-01-28 Thread MRAB
Ben Finney wrote: Howdy all, Python 2.6 tells me that, in Python 3, sorting a list with entries of incompatible types is not allowed: = $ python2.5 -c "foo = [1, True, 'green', 4, -27, 15.3]; foo.sort(); print foo;" [-27, 1, True, 4, 15.301, 'green'] $ python2.6 -c "foo = [1, T

Re: Sorting a list with entries of unequal types

2010-01-28 Thread Paul Rubin
Ben Finney writes: > So how should I be sorting a list with entries of “unequal types” such > that it will work in Python 3? Um, what ordering do you want? Basically you have to write a custom key function (they removed the option of writing a comparison function). Maybe something like foo

Re: Sorting a list with entries of unequal types

2010-01-28 Thread Peter Otten
Ben Finney wrote: > Python 2.6 tells me that, in Python 3, sorting a list with entries of > incompatible types is not allowed: > $ python2.6 -3 -c "foo = [1, True, 'green', 4, -27, 15.3]; foo.sort(); > print foo;" -c:1: DeprecationWarning: comparing unequal types not > supported in 3.x > [-27, 1,

Re: Sorting a list

2008-10-28 Thread duncan smith
RC wrote: > unsortedList = list(["XYZ","ABC"]) > > sortedList = unsortedList.sort() > print sortedList > > > Why this return None? Because the sort method sorts the list in place (and returns None). > How do I get return as ["ABC", "XYZ"]? >>> unsortedList = ["XYZ","ABC"] >>> unsortedList.sor

Re: Sorting a list

2008-10-28 Thread Brian Blais
On Oct 28, 2008, at 9:45 , RC wrote: unsortedList = list(["XYZ","ABC"]) sortedList = unsortedList.sort() print sortedList the sort method is in-place, so it modifies the object which calls it, and doesn't return anything: In [1]:unsortedList = list(["XYZ","ABC"]) In [2]:sortedList = uns

Re: Sorting a list

2008-10-28 Thread J Sisson
To expand on Tino's response, sort() sorts in place and does not *return* a sorted copy of the list. In other words: unsortedList = list(["XYZ","ABC"]) unsortedList.sort() print sortedList is correct. Since sort() returns None, you lose your list if you do: unsortedList = unsortedList.sort()

Re: Sorting a list

2008-10-28 Thread Steve Holden
RC wrote: > unsortedList = list(["XYZ","ABC"]) > > sortedList = unsortedList.sort() > print sortedList > > > Why this return None? > How do I get return as ["ABC", "XYZ"]? The list's .sort method returns None because it modifies the list in-place, so after the call it is sorted. So you can eith

Re: Sorting a list

2008-10-28 Thread Tino Wildenhain
RC wrote: unsortedList = list(["XYZ","ABC"]) sortedList = unsortedList.sort() print sortedList Why this return None? Because you did not read the documentation. Regards Tino smime.p7s Description: S/MIME Cryptographic Signature -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorting a List of Objects by an Attribute of the Objects Case-Insensitively

2008-04-09 Thread Jason
Thanks so much everyone! That works great, & (presumably) is way faster than the way I was doing it-- -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorting a List of Objects by an Attribute of the Objects Case-Insensitively

2008-04-09 Thread Peter Otten
Paddy wrote: > On Apr 9, 4:04 am, Jason <[EMAIL PROTECTED]> wrote: >> Hi folks-- >> >> Basically, I have a pressing need for a combination of 5.2 "Sorting a >> List of Strings Case-Insensitively" & 5.3 "Sorting a List of Objects >> by an Attribute of the Objects" from the Python Cookbook. >> >> My

Re: Sorting a List of Objects by an Attribute of the Objects Case-Insensitively

2008-04-08 Thread Paddy
On Apr 9, 4:04 am, Jason <[EMAIL PROTECTED]> wrote: > Hi folks-- > > Basically, I have a pressing need for a combination of 5.2 "Sorting a > List of Strings Case-Insensitively" & 5.3 "Sorting a List of Objects > by an Attribute of the Objects" from the Python Cookbook. > > My first guess isn't work

Re: Sorting a List of Objects by an Attribute of the Objects Case-Insensitively

2008-04-08 Thread Jason
On Apr 8, 8:26 pm, "David Harrison" <[EMAIL PROTECTED]> wrote: > On 09/04/2008, Jason <[EMAIL PROTECTED]> wrote: > > > Hi folks-- > > > Basically, I have a pressing need for a combination of 5.2 "Sorting a > > List of Strings Case-Insensitively" & 5.3 "Sorting a List of Objects > > by an Attribu

Re: Sorting a List of Objects by an Attribute of the Objects Case-Insensitively

2008-04-08 Thread David Harrison
On 09/04/2008, Jason <[EMAIL PROTECTED]> wrote: > Hi folks-- > > Basically, I have a pressing need for a combination of 5.2 "Sorting a > List of Strings Case-Insensitively" & 5.3 "Sorting a List of Objects > by an Attribute of the Objects" from the Python Cookbook. > > My first guess isn't work

Re: Sorting a list depending of the indexes of another sorted list

2008-01-21 Thread Peter Otten
Steven D'Aprano wrote: >> The following relies on undocumented (I hope) behaviour: > preferences = [10, 30, 20] > hosts = [ "mx1.domain.com", "anotherhost.domain.com", >>... "mx2.domain.com"] > hosts.sort(key=lambda x, p=iter(preferences).next: p()) > preferences.sort() > ho

Re: Sorting a list depending of the indexes of another sorted list

2008-01-21 Thread Steven D'Aprano
On Mon, 21 Jan 2008 09:53:10 +0100, Peter Otten wrote: > Santiago Romero wrote: > >> I'm trying to sort both lists so that they end like this: >> >> preferences = [10, 20, 30] >> hosts = [ "mx1.domain.com", "mx2.domain.com", "anotherhost.domain.com" >> ] >> >> I want to sort hosts list depen

Re: Sorting a list depending of the indexes of another sorted list

2008-01-21 Thread Steven D'Aprano
On Mon, 21 Jan 2008 17:32:42 +0800, J. Peng wrote: > Steven D'Aprano 写道: >> On Mon, 21 Jan 2008 16:23:50 +0800, J. Peng wrote: >> >>> J. Peng 写道: >>> k = (i.split())[3] y = (i.split())[1] >>> btw, why can't I write the above two into one statement? >>> >>> (k,y) = (i.split())[3,1]

Re: Sorting a list depending of the indexes of another sorted list

2008-01-21 Thread Santiago Romero
Thanks all for the answers ... I'll use a tuple as you said :) Anyway, is interesting to know how to sort 2 lists when you dont want to use tuples, so thanks also to Peter :) > Then one have to split the list twice.Given the list is large,it's maybe > not good for performance.Is it a more effe

Re: Sorting a list depending of the indexes of another sorted list

2008-01-21 Thread J. Peng
Steven D'Aprano 写道: > On Mon, 21 Jan 2008 16:23:50 +0800, J. Peng wrote: > >> J. Peng 写道: >> >>>k = (i.split())[3] >>>y = (i.split())[1] >> btw, why can't I write the above two into one statement? >> >> (k,y) = (i.split())[3,1] > > I don't know. What's "i"? > > I'm guessing "i" is a stri

Re: Sorting a list depending of the indexes of another sorted list

2008-01-21 Thread Steven D'Aprano
On Mon, 21 Jan 2008 16:23:50 +0800, J. Peng wrote: > J. Peng 写道: > >>k = (i.split())[3] >>y = (i.split())[1] > > btw, why can't I write the above two into one statement? > > (k,y) = (i.split())[3,1] I don't know. What's "i"? I'm guessing "i" is a string (and what a horrible choice of

Re: Sorting a list depending of the indexes of another sorted list

2008-01-21 Thread Peter Otten
Santiago Romero wrote: > I'm trying to sort both lists so that they end like this: > > preferences = [10, 20, 30] > hosts = [ "mx1.domain.com", "mx2.domain.com", > "anotherhost.domain.com" ] > > I want to sort hosts list depending on the numeric order of > "preferences". The following relies

Re: Sorting a list depending of the indexes of another sorted list

2008-01-21 Thread J. Peng
J. Peng 写道: >k = (i.split())[3] >y = (i.split())[1] btw, why can't I write the above two into one statement? (k,y) = (i.split())[3,1] -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorting a list depending of the indexes of another sorted list

2008-01-21 Thread J. Peng
I tried to write it below,it can work,:) v= """preference 10 host mx1.domain.com preference 30 host anotherhost.domain.com preference 20 host mx2.domain.com""" x=v.split("\n") li =[] for i in x: k = (i.split())[3] y = (i.split())[1] li.append((y,k)) li.sort() print li the output is:

Re: Sorting a list depending of the indexes of another sorted list

2008-01-20 Thread babui
On 21 ene, 08:41, Santiago Romero <[EMAIL PROTECTED]> wrote: > Hi ... > > I have the following DNS MX records info: > > domain.com > preference 10 host mx1.domain.com > preference 30 host anotherhost.domain.com > preference 20 host mx2.domain.com > And finally ... do you think there is a bette

Re: sorting a list of list

2007-11-21 Thread Matt Nordhoff
Tim Chase wrote: >> are there available library or pythonic algorithm for sorting a list >> of list depending on the index of the list inside the list of my >> choice? > > The built-in sorted() function and the sort() method on various > collections take an optional "key=function" keyword paramate

Re: sorting a list of list

2007-11-20 Thread Tim Chase
> are there available library or pythonic algorithm for sorting a list > of list depending on the index of the list inside the list of my > choice? The built-in sorted() function and the sort() method on various collections take an optional "key=function" keyword paramater with which you can pass

RE: sorting a list numbers stored as strings

2007-09-26 Thread Delaney, Timothy (Tim)
Hrvoje Niksic wrote: > "Delaney, Timothy (Tim)" <[EMAIL PROTECTED]> writes: > >> Yep - appears I must have been misremembering from another language >> (dunno which) > > Tcl Not bloody likely - only used Tcl for expect, and then only very minimally. I'm sure there's at least one language though

RE: sorting a list numbers stored as strings

2007-09-26 Thread Delaney, Timothy (Tim)
ZeD wrote: > thebjorn wrote: > >> int("020") >>> 20 >> 020 >>> 16 >> >> You can get the latter behavior using eval: > > why using eval when int has the "base" optional parameter? > int("020") > 20 int("020", 8) > 16 int("09", 8) > Traceback (most recent call last): > F

Re: sorting a list numbers stored as strings

2007-09-25 Thread ZeD
thebjorn wrote: >> >>> int("020") >> 20 >> >>> 020 >> 16 > > You can get the latter behavior using eval: why using eval when int has the "base" optional parameter? >>> int("020") 20 >>> int("020", 8) 16 >>> int("09", 8) Traceback (most recent call last): File "", line 1, in ValueError: inval

Re: sorting a list numbers stored as strings

2007-09-25 Thread thebjorn
On Sep 25, 2:45 pm, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > On Tue, 25 Sep 2007 12:46:54 +0800, Delaney, Timothy (Tim) wrote: > > Carsten Haese wrote: > > >> On Mon, 2007-09-24 at 19:58 +0800, Delaney, Timothy (Tim) wrote: > >>> I'm sure that in some version of Python it wou

Re: sorting a list numbers stored as strings

2007-09-25 Thread Hrvoje Niksic
"Delaney, Timothy (Tim)" <[EMAIL PROTECTED]> writes: > Yep - appears I must have been misremembering from another language > (dunno which) Tcl -- http://mail.python.org/mailman/listinfo/python-list

Re: sorting a list numbers stored as strings

2007-09-25 Thread Steven D'Aprano
On Tue, 25 Sep 2007 12:46:54 +0800, Delaney, Timothy (Tim) wrote: > Carsten Haese wrote: > >> On Mon, 2007-09-24 at 19:58 +0800, Delaney, Timothy (Tim) wrote: >>> I'm sure that in some version of Python it would have given a >>> ValueError (due to the default radix being 0) but it appears to have

Re: sorting a list numbers stored as strings

2007-09-24 Thread Amit Khemka
On 9/24/07, Carsten Haese <[EMAIL PROTECTED]> wrote: > On Mon, 2007-09-24 at 16:53 +0530, Amit Khemka wrote: > > On 9/24/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > > > >>> l = ["1", "11", "2", "22"] > > >>> sorted(l, cmp = lambda x, y: cmp(int(x), int(y))) # provide your > > own compare

RE: sorting a list numbers stored as strings

2007-09-24 Thread Delaney, Timothy (Tim)
Carsten Haese wrote: > On Mon, 2007-09-24 at 19:58 +0800, Delaney, Timothy (Tim) wrote: >> I'm sure that in some version of Python it would have given a >> ValueError (due to the default radix being 0) but it appears to have >> changed to a default radix of 10 somewhere along the way. > > Not eve

Re: sorting a list numbers stored as strings

2007-09-24 Thread Michael J. Fromberger
In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] wrote: > hi, > > I have the following list - > > ["1", "11", "2", "22"] > > how do I sort it like this - > > ["1", "2", "11", "22"] > > thanks, > > aine Try: lst.sort(key = lambda s: int(s)) Assuming, of course, that "lst" is your origi

RE: sorting a list numbers stored as strings

2007-09-24 Thread Carsten Haese
On Mon, 2007-09-24 at 19:58 +0800, Delaney, Timothy (Tim) wrote: > I'm sure that in some version of Python it would have given a > ValueError (due to the default radix being 0) but it appears to have > changed to a default radix of 10 somewhere along the way. Not even Python 1.5.2 seems to have a

Re: sorting a list numbers stored as strings

2007-09-24 Thread Peter Otten
aine_canby wrote: > I have the following list - > > ["1", "11", "2", "22"] > > how do I sort it like this - > > ["1", "2", "11", "22"] >>> items = ["1", "11", "2", "22"] >>> items.sort(key=int) >>> items ['1', '2', '11', '22'] This is more efficient than Amit's compare function and even Bruno

RE: sorting a list numbers stored as strings

2007-09-24 Thread Delaney, Timothy (Tim)
Carsten Haese wrote: > That interpreter session is a work of fiction, since sorted returns > the sorted list instead of sorting the list in place. Also, it's > better (i.e. more readable and likely faster) to use a sort key > function instead of a comparison function whenever possible. In this > c

Re: sorting a list numbers stored as strings

2007-09-24 Thread Carsten Haese
On Mon, 2007-09-24 at 16:53 +0530, Amit Khemka wrote: > On 9/24/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > hi, > > > > I have the following list - > > > > ["1", "11", "2", "22"] > > > > how do I sort it like this - > > > > ["1", "2", "11", "22"] > > > > Hi, > > >>> l = ["1", "11", "2",

Re: sorting a list numbers stored as strings

2007-09-24 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit : > hi, > > I have the following list - > > ["1", "11", "2", "22"] > > how do I sort it like this - > > ["1", "2", "11", "22"] source = ["1", "11", "2", "22"] result = [t[1] for t in sorted((int(item), item) for item in source)] print result -- http://mail.python.or

Re: sorting a list numbers stored as strings

2007-09-24 Thread Amit Khemka
On 9/24/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > hi, > > I have the following list - > > ["1", "11", "2", "22"] > > how do I sort it like this - > > ["1", "2", "11", "22"] > Hi, >>> l = ["1", "11", "2", "22"] >>> sorted(l, cmp = lambda x, y: cmp(int(x), int(y))) # provide your own comp

Re: Sorting a list of Unicode strings?

2007-08-30 Thread koen . vanleemput
Wikipedia in Suomi lists it at the bottom ;-) http://sv.wikipedia.org/wiki/Lista_%C3%B6ver_l%C3%A4nder#.C3.85 Cheers ~K -- http://mail.python.org/mailman/listinfo/python-list

Re: sorting a list of lists

2007-08-28 Thread nicksavill
Thanks guys, "dirty hack" was what I needed to get a job done quickly. Nick -- http://mail.python.org/mailman/listinfo/python-list

Re: sorting a list of lists

2007-08-27 Thread Gabriel Genellina
En Mon, 27 Aug 2007 15:41:15 -0300, iapain <[EMAIL PROTECTED]> escribi�: >> Taking advantage of stable sorting is totally not a hack. The OP just >> tried >> the two sorting steps in the wrong order. > > I didnt say not to use stable sorting, but write a generic function > and hacky code. It is

Re: sorting a list of lists

2007-08-27 Thread Tuomas
>>> records = [['dog',1,2], ['chair',2,1], ['cat',1,3], ['horse',3,4], ... ['table',3,2], ['window',3,5]] >>> sorted(records, key = lambda x: (x[1], x[2])) [['dog', 1, 2], ['cat', 1, 3], ['chair', 2, 1], ['table', 3, 2], ['horse', 3, 4], ['window', 3, 5]] [EMAIL PROTECTED] wrote: > Hi, > > i

Re: sorting a list of lists

2007-08-27 Thread [EMAIL PROTECTED]
On Aug 27, 2:41 pm, iapain <[EMAIL PROTECTED]> wrote: > ... It is always better to adopt a generic approach. The above statement is incorrect. -- http://mail.python.org/mailman/listinfo/python-list

Re: sorting a list of lists

2007-08-27 Thread iapain
> Taking advantage of stable sorting is totally not a hack. The OP just tried > the two sorting steps in the wrong order. I didnt say not to use stable sorting, but write a generic function and hacky code. It is always better to adopt a generic approach. -- http://mail.python.org/mailman/listin

Re: sorting a list of lists

2007-08-27 Thread Stefan Behnel
iapain wrote: >> i would like to sort a list of lists. The list is first sorted on the >> second item in the sub-lists (which I can do), then on the third item >> (which I can't). > > Write a comparator instead of dirty hacks > > mylistoflist.sort(mycomparator) > > def mycomparator(a, b): > #d

Re: sorting a list of lists

2007-08-27 Thread iapain
> i would like to sort a list of lists. The list is first sorted on the > second item in the sub-lists (which I can do), then on the third item > (which I can't). Write a comparator instead of dirty hacks mylistoflist.sort(mycomparator) def mycomparator(a, b): #do -- http://mail.python.org/

Re: sorting a list of lists

2007-08-27 Thread Peter Otten
[EMAIL PROTECTED] wrote: > i would like to sort a list of lists. The list is first sorted on the > second item in the sub-lists (which I can do), then on the third item > (which I can't). > > eg. records = [['dog',1,2], ['chair',2,1], ['cat',1,3], ['horse',3,4], > ['table',3,2], ['window',3,5]]

Re: Sorting a list of Unicode strings?

2007-08-20 Thread [EMAIL PROTECTED]
Thank you all for your very quick and informative replies. I was basing this assumption that Å was classed as a standard 'A' from a list of countries I was looking at (Wikipedia sorts it like this, too - though this isn't what I was using http://en.wikipedia.org/wiki/List_of_countries#A) I will l

Re: Sorting a list of Unicode strings?

2007-08-20 Thread Tommy Nordgren
On 19 aug 2007, at 18.50, [EMAIL PROTECTED] wrote: > Hey Guys, > > Maybe I'm missing something fundamental here, but if I have a list of > Unicode strings, and I want to sort these alphabetically, then it > places those that begin with unicode characters at the bottom. Is > there a way to avoid t

Re: Sorting a list of Unicode strings?

2007-08-19 Thread thebjorn
On Aug 19, 8:09 pm, [EMAIL PROTECTED] (Alex Martelli) wrote: [...] > In both Swedish and Danish, I believe, A-with-ring sorts AFTER the > letter Z in the alphabet; so, having Åaland (where I'm using Aa for > A-with-ring, since this newsreader has some problem in letting me enter > non-ascii charact

Re: Sorting a list of Unicode strings?

2007-08-19 Thread Steve Holden
Alex Martelli wrote: > [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: >... Maybe I'm missing something fundamental here, but if I have a list of Unicode strings, and I want to sort these alphabetically, then it places those that begin with unicode characters at the bottom. >...

Re: Sorting a list of Unicode strings?

2007-08-19 Thread Alex Martelli
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: ... > > > Maybe I'm missing something fundamental here, but if I have a list of > > > Unicode strings, and I want to sort these alphabetically, then it > > > places those that begin with unicode characters at the bottom. ... > Anyway, I know _why_

Re: Sorting a list of Unicode strings?

2007-08-19 Thread [EMAIL PROTECTED]
On Aug 19, 6:01 pm, Stefan Behnel <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > Hey Guys, > > ... and girls - maybe ... > > > Maybe I'm missing something fundamental here, but if I have a list of > > Unicode strings, and I want to sort these alphabetically, then it > > places those that

Re: Sorting a list of Unicode strings?

2007-08-19 Thread Stefan Behnel
[EMAIL PROTECTED] wrote: > Hey Guys, ... and girls - maybe ... > Maybe I'm missing something fundamental here, but if I have a list of > Unicode strings, and I want to sort these alphabetically, then it > places those that begin with unicode characters at the bottom. That's because "Unicode" is

Re: Sorting a list

2007-02-02 Thread John Salerno
Bruno Desthuilliers wrote: > John Salerno a écrit : > (snip) > >> Oh I didn't sort then reverse, I just replaced sort with reverse. >> Maybe that's why! > > Hmmm... Probably, yes... > > !-) lol, this is what a couple months away from python does to me! -- http://mail.python.org/mailman/listin

Re: Sorting a list

2007-02-02 Thread Duncan Booth
Steven Bethard <[EMAIL PROTECTED]> wrote: > You don't need to use sorted() -- sort() also takes the key= and > reverse= arguments:: > > >>> lines = [('1995', 'aaa'), ('1997', 'bbb'), ('1995', 'bbb'), > ... ('1997', 'aaa'), ('1995', 'ccc'), ('1996', 'ccc'), > ...

Re: Sorting a list

2007-02-01 Thread Steven D'Aprano
On Thu, 01 Feb 2007 14:52:03 -0500, John Salerno wrote: > Bruno Desthuilliers wrote: > >> You don't tell how these lines are formatted, but it's possible that you >> don't even need a regexp here. But wrt/ sorting, the list of tuples with >> the sort key as first element is one of the best solu

Re: Sorting a list

2007-02-01 Thread Ben Finney
John Salerno <[EMAIL PROTECTED]> writes: > Ah, so simply using sort() [on a list of tuples] will default to the > first element of each tuple? More precisely, list.sort will ask the elements of the list to compare themselves. Those elements are tuples; two tuples will compare based on comparison

Re: Sorting a list

2007-02-01 Thread Bruno Desthuilliers
John Salerno a écrit : (snip) > Oh I didn't sort then reverse, I just replaced sort with reverse. Maybe > that's why! Hmmm... Probably, yes... !-) -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorting a list

2007-02-01 Thread John Salerno
Bruno Desthuilliers wrote: >> One more thing. What if I want them in reverse chronological order? I >> tried reverse() but that seemed to put them in reverse alphabetical >> order based on the second element of the tuple (not the year). > > Really ? > > >>> lines = [('1995', 'aaa'), ('1997',

Re: Sorting a list

2007-02-01 Thread Bruno Desthuilliers
Steven Bethard a écrit : > Bruno Desthuilliers wrote: > >> If you want to prevent this from happening and don't mind creating a >> copy of the list, you can use the sorted() function with the key and >> reverse arguments and operator.itemgetter: >> (snip) > > You don't need to use sorted() -- s

Re: Sorting a list

2007-02-01 Thread Steven Bethard
Bruno Desthuilliers wrote: > If you want to prevent this from happening and don't mind creating a > copy of the list, you can use the sorted() function with the key and > reverse arguments and operator.itemgetter: > > >>> lines = [('1995', 'aaa'), ('1997', 'bbb'), ('1995', 'bbb'), > ('1997', '

Re: Sorting a list

2007-02-01 Thread Bruno Desthuilliers
John Salerno a écrit : > John Salerno wrote: > >> Bruno Desthuilliers wrote: >> >>> John Salerno a écrit : >>> Hi everyone. If I have a list of tuples, and each tuple is in the form: (year, text) as in ('1995', 'This is a citation.') How can I sort the list so that they ar

Re: Sorting a list

2007-02-01 Thread Bruno Desthuilliers
John Salerno a écrit : > Bruno Desthuilliers wrote: > >> John Salerno a écrit : >> >>> Hi everyone. If I have a list of tuples, and each tuple is in the form: >>> >>> (year, text) as in ('1995', 'This is a citation.') >>> >>> How can I sort the list so that they are in chronological order based >

Re: Sorting a list

2007-02-01 Thread Bruno Desthuilliers
John Salerno a écrit : > Bruno Desthuilliers wrote: > >> You don't tell how these lines are formatted, but it's possible that >> you don't even need a regexp here. But wrt/ sorting, the list of >> tuples with the sort key as first element is one of the best solutions. > > > Ah, so simply using

RE: Sorting a list

2007-02-01 Thread Carroll, Barry
> -Original Message- > From: John Salerno [mailto:[EMAIL PROTECTED] > Sent: Thursday, February 01, 2007 11:54 AM > To: python-list@python.org > Subject: Re: Sorting a list > > Bruno Desthuilliers wrote: > > John Salerno a écrit : > >> Hi everyone.

Re: Sorting a list

2007-02-01 Thread John Salerno
John Salerno wrote: > Bruno Desthuilliers wrote: >> John Salerno a écrit : >>> Hi everyone. If I have a list of tuples, and each tuple is in the form: >>> >>> (year, text) as in ('1995', 'This is a citation.') >>> >>> How can I sort the list so that they are in chronological order based >>> on the

Re: Sorting a list

2007-02-01 Thread John Salerno
John Salerno wrote: > Bruno Desthuilliers wrote: >> John Salerno a écrit : >>> Hi everyone. If I have a list of tuples, and each tuple is in the form: >>> >>> (year, text) as in ('1995', 'This is a citation.') >>> >>> How can I sort the list so that they are in chronological order based >>> on the

Re: Sorting a list

2007-02-01 Thread John Salerno
Bruno Desthuilliers wrote: > John Salerno a écrit : >> Hi everyone. If I have a list of tuples, and each tuple is in the form: >> >> (year, text) as in ('1995', 'This is a citation.') >> >> How can I sort the list so that they are in chronological order based >> on the year? > > Calling sort() on

Re: Sorting a list

2007-02-01 Thread John Salerno
Bruno Desthuilliers wrote: > You don't tell how these lines are formatted, but it's possible that you > don't even need a regexp here. But wrt/ sorting, the list of tuples with > the sort key as first element is one of the best solutions. Ah, so simply using sort() will default to the first ele

  1   2   >