[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Sorting a list of lists

R (Chandra) Chandrasekhar

2/12/2010 5:46:00 PM

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 have appended each line into a list L so that I for teh
above minimal data, I have a list of lists thus:

[[14, [25, 105, 104]], [10, [107, 106, 162]], [21, [26, 116, 165]]]

I have tried using

(a) sorted(L, key = lambda x:(x[0]))

and

(b) sorted(L, key = lambda x:(sum(x[1])))

and get the anticipated results for (a) and (b0, at least with the above
minimal data set.

Is this a sensible way to go about the sorting, or are there better ways
of doing it in Python?

Also, I am baffled because the above fails obviously when len(L) is
about a hundred and I can't figure out why.

TIA

Chandra
3 Answers

MRAB

2/12/2010 6:24:00 PM

0

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
> a list in itself.
>
> At present, I have appended each line into a list L so that I for teh
> above minimal data, I have a list of lists thus:
>
> [[14, [25, 105, 104]], [10, [107, 106, 162]], [21, [26, 116, 165]]]
>
> I have tried using
>
> (a) sorted(L, key = lambda x:(x[0]))
>
> and
>
> (b) sorted(L, key = lambda x:(sum(x[1])))
>
> and get the anticipated results for (a) and (b0, at least with the above
> minimal data set.
>
> Is this a sensible way to go about the sorting, or are there better ways
> of doing it in Python?
>
It's the obvious way to do it.

> Also, I am baffled because the above fails obviously when len(L) is
> about a hundred and I can't figure out why.
>
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

2/12/2010 6:33:00 PM

0

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 in each list, which is
> a list in itself.
>
> At present, I have appended each line into a list L so that I for teh
> above minimal data, I have a list of lists thus:
>
> [[14, [25, 105, 104]], [10, [107, 106, 162]], [21, [26, 116, 165]]]
>
> I have tried using
>
> (a) sorted(L, key = lambda x:(x[0]))

Just plain "sorted(L)" will work, because Python knows how to compare
your [N, [N,N,N]]-format values:

>>> [14, [25, 105, 104]] < (14, [25, 105, 103]]
False
>>> [14, [25, 105, 104]] < (14, [25, 105, 105]]
True
>>>

>
> and
>
> (b) sorted(L, key = lambda x:(sum(x[1])))

That looks good.

>
> and get the anticipated results for (a) and (b0, at least with the above
> minimal data set.
>
> Is this a sensible way to go about the sorting, or are there better ways
> of doing it in Python?

You're doing fine.

>
> Also, I am baffled because the above fails obviously when len(L) is
> about a hundred and I can't figure out why.

Please cut-and-paste the exact error message (or other evidence of
"failure") into a message.


Tx,
John

R (Chandra) Chandrasekhar

2/14/2010 3:19:00 AM

0

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 message.

Thank you both. It appears that the problem was in the way the data were
being read in from a file, than in the sorting itself. Once I fixed
that, the results are as expected.

Chandra