How can I sort a list of lists by one of the items in the sublists?
Question:
I have a list which looks as follows:
list = [[u'scheixdfunwetter', u'storm', u'0.0007813'], [u'scheixdfunwetter', u'goddamn', u'0.0002343'], [u'ingenieurin', u'night', u'0.0000442'], [u'ingenieurin', u'you', u'0.0000008'], [u'ingenieurin', u'NULL', u'0.0000004'], [u'ingenieurin', u'last', u'0.0000472'], [u'ingenieurin', u'anything', u'0.0000328']]
I’d like to sort this list by the number (=probability).
Looking through previous questions/answers on Stackoverflow, I found the following solution, which seems to have worked for the list (li = [['a1', 1, 1.56], ['b3', '6', 9.28], ['c2', 1, 6.25]...]
):
list.sort(key=itemgetter(2))
With my data however, this does not work and returns NONE.
As I thought that it had to do with the number being a string, I tried the following:
sorted_list = list.sort(key=lambda x: int(x[2]))
, but this returns another error:
ValueError: invalid literal for int() with base 10: '0.0000723'
Can anyone help?
Answers:
In [24]: sorted(seq, key=lambda x: float(x[2]))
Out[24]:
[[u'ingenieurin', u'NULL', u'0.0000004'],
[u'ingenieurin', u'you', u'0.0000008'],
[u'ingenieurin', u'anything', u'0.0000328'],
[u'ingenieurin', u'night', u'0.0000442'],
[u'ingenieurin', u'last', u'0.0000472'],
[u'scheixdfunwetter', u'goddamn', u'0.0002343'],
[u'scheixdfunwetter', u'storm', u'0.0007813']]
- Use
float
instead of int
.
- Use the
sorted
function instead of the sort
method. The sort method
returns None and modifies the list in place. The sorted
function returns a sorted copy of the list. If you want to assign
the result to sorted_list
, then you need to use the sorted
function.
- Never name a variable
list
, since it shadows the builtin of the
same name.
You have to try
>>> list.sort(key=lambda x: x[2])
>>> list
[[u'ingenieurin', u'NULL', u'0.0000004'], [u'ingenieurin', u'you', u'0.0000008'], [u'ingenieurin', u'anything', u'0.0000328'], [u'ingenieurin', u'night', u'0.0000442'], [u'ingenieurin', u'last', u'0.0000472'], [u'scheixdfunwetter', u'goddamn', u'0.0002343'], [u'scheixdfunwetter', u'storm', u'0.0007813']]
list.sort
will perform operation on self. It will return None. If you have to create new sorted list then you have to use sorted
inbuilt function.
I have a list which looks as follows:
list = [[u'scheixdfunwetter', u'storm', u'0.0007813'], [u'scheixdfunwetter', u'goddamn', u'0.0002343'], [u'ingenieurin', u'night', u'0.0000442'], [u'ingenieurin', u'you', u'0.0000008'], [u'ingenieurin', u'NULL', u'0.0000004'], [u'ingenieurin', u'last', u'0.0000472'], [u'ingenieurin', u'anything', u'0.0000328']]
I’d like to sort this list by the number (=probability).
Looking through previous questions/answers on Stackoverflow, I found the following solution, which seems to have worked for the list (li = [['a1', 1, 1.56], ['b3', '6', 9.28], ['c2', 1, 6.25]...]
):
list.sort(key=itemgetter(2))
With my data however, this does not work and returns NONE.
As I thought that it had to do with the number being a string, I tried the following:
sorted_list = list.sort(key=lambda x: int(x[2]))
, but this returns another error:
ValueError: invalid literal for int() with base 10: '0.0000723'
Can anyone help?
In [24]: sorted(seq, key=lambda x: float(x[2]))
Out[24]:
[[u'ingenieurin', u'NULL', u'0.0000004'],
[u'ingenieurin', u'you', u'0.0000008'],
[u'ingenieurin', u'anything', u'0.0000328'],
[u'ingenieurin', u'night', u'0.0000442'],
[u'ingenieurin', u'last', u'0.0000472'],
[u'scheixdfunwetter', u'goddamn', u'0.0002343'],
[u'scheixdfunwetter', u'storm', u'0.0007813']]
- Use
float
instead ofint
. - Use the
sorted
function instead of thesort
method. The sort method
returns None and modifies the list in place. Thesorted
function returns a sorted copy of the list. If you want to assign
the result tosorted_list
, then you need to use thesorted
function. - Never name a variable
list
, since it shadows the builtin of the
same name.
You have to try
>>> list.sort(key=lambda x: x[2])
>>> list
[[u'ingenieurin', u'NULL', u'0.0000004'], [u'ingenieurin', u'you', u'0.0000008'], [u'ingenieurin', u'anything', u'0.0000328'], [u'ingenieurin', u'night', u'0.0000442'], [u'ingenieurin', u'last', u'0.0000472'], [u'scheixdfunwetter', u'goddamn', u'0.0002343'], [u'scheixdfunwetter', u'storm', u'0.0007813']]
list.sort
will perform operation on self. It will return None. If you have to create new sorted list then you have to use sorted
inbuilt function.