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

sorting a list of dicts by a computed field

2017-01-31 Thread Larry Martell
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 /report/CDSEM/WaferAlignment/ajax/waChart.jso

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)

Sorting a list

2016-04-03 Thread DFS
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) , (cntDate,strDate) , (cntBins,strB

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

Sorting a list created from a parsed xml message

2010-07-21 Thread kak...@gmail.com
Hi pythonistas, >From the subject of my message it's clear that i get an xml message from a socket, i parse it and the result is a list like the one that follows: ID_Col 4 Serverak ip OFFLINE 29 Server and2ip OFFLINE 5 Proxy l34e

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

Sorting a list of lists

2010-02-12 Thread R (Chandra) Chandrasekhar
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 a list in itself. At present, I

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?

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 t

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

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

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&#x

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 somethin

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

Sorting a list with entries of unequal types

2010-01-28 Thread Ben Finney
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

Re: Trouble sorting a list of objects by attributes

2009-02-06 Thread Robocop
On Feb 6, 2:41 pm, Stephen Hansen wrote: > > I think there may have been a misunderstanding.  I was already using > > attrgetter, my problem is that it doesn't appear to be sorting by the > > argument i give it.  How does sort work with strings?  How about with > > datetime.time or datetime.date?

Re: Trouble sorting a list of objects by attributes

2009-02-06 Thread Stephen Hansen
> I think there may have been a misunderstanding. I was already using > attrgetter, my problem is that it doesn't appear to be sorting by the > argument i give it. How does sort work with strings? How about with > datetime.time or datetime.date? You were using the attrgetter, but it looks like

Re: Trouble sorting a list of objects by attributes

2009-02-06 Thread Robocop
On Feb 6, 2:34 pm, Robocop wrote: > On Feb 6, 2:20 pm, Robocop wrote: > > > > > On Feb 6, 2:17 pm, Robocop wrote: > > > > On Feb 6, 1:03 pm, bearophileh...@lycos.com wrote: > > > > > Robocop: > > > > > >then within each department block of the list, have it organized by > > > > >projects.< > >

Re: Trouble sorting a list of objects by attributes

2009-02-06 Thread Terry Reedy
Robocop wrote: UH OH GUYS! line 110, in sorter timesheets.sort(key=attrgetter("department", "engagement", "date","start")) TypeError: attrgetter expected 1 arguments, got 4 Um... what version of Python are you running? Alway specify. (Too many people do not). In 3.0 from operator imp

Re: Trouble sorting a list of objects by attributes

2009-02-06 Thread Robocop
On Feb 6, 2:20 pm, Robocop wrote: > On Feb 6, 2:17 pm, Robocop wrote: > > > > > On Feb 6, 1:03 pm, bearophileh...@lycos.com wrote: > > > > Robocop: > > > > >then within each department block of the list, have it organized by > > > >projects.< > > > > I don't know what does it means. > > > > > ti

Re: Trouble sorting a list of objects by attributes

2009-02-06 Thread Robocop
On Feb 6, 2:17 pm, Robocop wrote: > On Feb 6, 1:03 pm, bearophileh...@lycos.com wrote: > > > > > Robocop: > > > >then within each department block of the list, have it organized by > > >projects.< > > > I don't know what does it means. > > > > timesheets.sort(key=operator.attrgetter('string')) >

Re: Trouble sorting a list of objects by attributes

2009-02-06 Thread Robocop
On Feb 6, 1:03 pm, bearophileh...@lycos.com wrote: > Robocop: > > >then within each department block of the list, have it organized by > >projects.< > > I don't know what does it means. > > > timesheets.sort(key=operator.attrgetter('string')) > > Try something like: > timesheets.sort(key=attrgette

Re: Trouble sorting a list of objects by attributes

2009-02-06 Thread Robocop
On Feb 6, 1:03 pm, bearophileh...@lycos.com wrote: > Robocop: > > >then within each department block of the list, have it organized by > >projects.< > > I don't know what does it means. > > > timesheets.sort(key=operator.attrgetter('string')) > > Try something like: > timesheets.sort(key=attrgette

Re: Trouble sorting a list of objects by attributes

2009-02-06 Thread bearophileHUGS
Robocop: >then within each department block of the list, have it organized by projects.< I don't know what does it means. > timesheets.sort(key=operator.attrgetter('string')) Try something like: timesheets.sort(key=attrgetter("department", "engagement", "date", "stare_hour")) > My brain might

Re: Trouble sorting a list of objects by attributes

2009-02-06 Thread Miki
> I've found myself stumped when trying to organize this list of > objects.  The objects in question are timesheets which i'd like to > sort by four attributes: > > class TimeSheet: >   department = string >   engagement = string >   date = datetime.date >   stare_hour = datetime.time > > My ultim

Trouble sorting a list of objects by attributes

2009-02-06 Thread rdmurray
Quoth Robocop : > Hello again, > I've found myself stumped when trying to organize this list of > objects. The objects in question are timesheets which i'd like to > sort by four attributes: > > class TimeSheet: > department = string > engagement = string > date = datetime.date > stare_ho

Trouble sorting a list of objects by attributes

2009-02-06 Thread Robocop
Hello again, I've found myself stumped when trying to organize this list of objects. The objects in question are timesheets which i'd like to sort by four attributes: class TimeSheet: department = string engagement = string date = datetime.date stare_hour = datetime.time My ultimate goal

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

Sorting a list

2008-10-28 Thread RC
unsortedList = list(["XYZ","ABC"]) sortedList = unsortedList.sort() print sortedList Why this return None? How do I get return as ["ABC", "XYZ"]? -- 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 >>

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

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-Inse

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 t

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

2008-04-08 Thread Jason
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 working: import operator def sort

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

Sorting a list depending of the indexes of another sorted list

2008-01-20 Thread Santiago Romero
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 I'm storing this info in 2 lists: preferences = [10, 30, 20] hosts = [ "mx1.domain.com", "anotherhost.domain.com", "mx2.d

Re: Very basic, sorting a list ???

2007-11-29 Thread self.mrlimbo
On 29 nov, 04:11, Hrvoje Niksic <[EMAIL PROTECTED]> wrote: > Stef Mientki <[EMAIL PROTECTED]> writes: > > although I find it rather non-intuitive. > > I didn't expect a copy, but a reference to itself wouldn't be asked > > too much ? > > If you didn't expect a copy, why rely on the return value? Y

Re: Very basic, sorting a list ???

2007-11-29 Thread Stef Mientki
Hrvoje Niksic wrote: > Stef Mientki <[EMAIL PROTECTED]> writes: > >> although I find it rather non-intuitive. >> I didn't expect a copy, but a reference to itself wouldn't be asked >> too much ? > > If you didn't expect a copy, why rely on the return value? You could > simply continue using the

Re: Very basic, sorting a list ???

2007-11-29 Thread Hrvoje Niksic
Stef Mientki <[EMAIL PROTECTED]> writes: > although I find it rather non-intuitive. > I didn't expect a copy, but a reference to itself wouldn't be asked > too much ? If you didn't expect a copy, why rely on the return value? You could simply continue using the sorted list. Your first post says

Re: Very basic, sorting a list ???

2007-11-29 Thread cokofreedom
On Nov 29, 10:40 am, Stef Mientki <[EMAIL PROTECTED]> wrote: > Peter Decker wrote: > > On Nov 28, 2007 7:22 PM, stef mientki <[EMAIL PROTECTED]> wrote: > >> print 'xx3',ordered_list.sort() > > > The sort() method returns None. It sorts the list in place; it doesn't > > return a copy of the sort

Re: Very basic, sorting a list ???

2007-11-29 Thread Stef Mientki
Peter Decker wrote: > On Nov 28, 2007 7:22 PM, stef mientki <[EMAIL PROTECTED]> wrote: >> print 'xx3',ordered_list.sort() > > The sort() method returns None. It sorts the list in place; it doesn't > return a copy of the sorted list. > Thank you all for the answers, I do understand now, althou

Re: Very basic, sorting a list ???

2007-11-28 Thread i3dmaster
On Nov 28, 4:22 pm, stef mientki <[EMAIL PROTECTED]> wrote: > hello, > > I'm trying to sort a list, using the same list at the commandline works, > but in a program it doesn't. > > Here is the code > print 'xx1',type(ordered_list) > print 'xx2',ordered_list > print 'xx3',ordered_list.so

Re: Very basic, sorting a list ???

2007-11-28 Thread Peter Decker
On Nov 28, 2007 7:22 PM, stef mientki <[EMAIL PROTECTED]> wrote: > print 'xx3',ordered_list.sort() The sort() method returns None. It sorts the list in place; it doesn't return a copy of the sorted list. -- # p.d. -- http://mail.python.org/mailman/listinfo/python-list

Re: Very basic, sorting a list ???

2007-11-28 Thread Ben Finney
stef mientki <[EMAIL PROTECTED]> writes: > Here is the code >print 'xx1',type(ordered_list) >print 'xx2',ordered_list >print 'xx3',ordered_list.sort() > > And this is the result > xx1 > xx2 [14, 12, 10] > xx3 None > > What am I doing wrong ? Apparently, ignoring the documentation o

Very basic, sorting a list ???

2007-11-28 Thread stef mientki
hello, I'm trying to sort a list, using the same list at the commandline works, but in a program it doesn't. Here is the code print 'xx1',type(ordered_list) print 'xx2',ordered_list print 'xx3',ordered_list.sort() And this is the result xx1 xx2 [14, 12, 10] xx3 None What am I doing

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 opti

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

sorting a list of list

2007-11-20 Thread james_027
hi, 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? d_list = [ ['a', 1, 9], ['b', 2, 8], ['c', 3, 7], ['d', 4, 6], ['e', 5, 5],

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

sorting a list numbers stored as strings

2007-09-24 Thread aine_canby
hi, I have the following list - ["1", "11", "2", "22"] how do I sort it like this - ["1", "2", "11", "22"] thanks, aine -- http://mail.python.org/mailman/listinfo/python-list

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

  1   2   3   >