By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,220 Members | 1,542 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,220 IT Pros & Developers. It's quick & easy.

How to sort a list of tuples

P: n/a
I have a list of tuples, and one of the fields in the tuple is score. So how can
I sort the list by the score?

Thanks in advance
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Fri, 2004-11-19 at 18:22, Valkyrie wrote:
I have a list of tuples, and one of the fields in the tuple is score. So how can
I sort the list by the score?


Assuming the score field is index 1 of each tuple:

def cmp(a,b):
if a[1] < b[1]:
return -1
elif a[1] > b[1]:
return 1
else:
return 0

my_tuple_list.sort(cmp)

(technically the elifs could be ifs, and the final else could be omitted
in favour of just 'return 0', but for clarity the above is IMO best).

see 'help(list.sort)' for more information.

To whoever added this fantastic feature, thanks and more thanks. It's
saved me so much work at times that it's just crazy.

--
Craig Ringer

Jul 18 '05 #2

P: n/a
On Fri, 19 Nov 2004 18:22:45 +0800
Valkyrie <va******@cuhk.edu.hk> wrote:
I have a list of tuples, and one of the fields in the tuple is score. So how can
I sort the list by the score?


In 2.4 you can use key argument of sort method:
l = [('a', 2), ('c', 3), ('b', 1)]
l.sort(key=lambda i: i[1])
l [('b', 1), ('a', 2), ('c', 3)]

otherwise pass comparison function: l = [('a', 2), ('c', 3), ('b', 1)]
l.sort(lambda i1, i2: cmp(i1[1], i2[0]))
l

[('b', 1), ('c', 3), ('a', 2)]

--
Denis S. Otkidach
http://www.python.ru/ [ru]
Jul 18 '05 #3

P: n/a
Thank you all, it's perfectly fine now :)

Valkyrie wrote:
I have a list of tuples, and one of the fields in the tuple is score. So how can
I sort the list by the score?

Thanks in advance

Jul 18 '05 #4

P: n/a
On Fri, 19 Nov 2004 18:22:45 +0800, rumours say that Valkyrie
<va******@cuhk.edu.hk> might have written:
I have a list of tuples, and one of the fields in the tuple is score. So how can
I sort the list by the score?


Python 2.4:

def sort_tuple_list(tuple_list, index_of_score):
tuple_list.sort(key=operator.itemgetter(index_of_s core))

If for example your tuple_list is of the format (v1, score, v3, v4),
then you sort by

sort_tuple_list(tuple_list, 1)

1 is the second item (0 is the first).

actual example:
import operator
tuple_list= [ ('chris', 15),
('pers6', 3),
('pers1', 56),
] sort_tuple_list(tuple_list, 1)
tuple_list

[('pers6', 3), ('chris', 15), ('pers1', 56)]
--
TZOTZIOY, I speak England very best,
"Tssss!" --Brad Pitt as Achilles in unprecedented Ancient Greek
Jul 18 '05 #5

P: n/a
Valkyrie wrote:
I have a list of tuples, and one of the fields in the tuple is score. So how can
I sort the list by the score?

Thanks in advance


Others have showed the possibility of a comparison function, but often
it's faster to use the decorate-sort-undecorate pattern:

def sort_on(list_to_sort, field_num):
templist = [ (item[field_num], item) for item in list_to_sort ]
templist.sort()
return [ item[1] for item in templist ]

Note that I'm creating a new list, leaving the original list unsorted.
You can easily rebind the original name to the sorted list if needed --

mylist = sort_on(mylist, 3)

I believe that using the key=... in 2.4 is faster than this DSU pattern,
but DSU is typically faster than using a cmp() function/lambda.

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.