I am trying to sort a list of lists based on the 2nd element in the sub list.
Sample Data:
[[u'm3u33mm534o', 14, 23], [u'2w3dfbv333g', 20, 34], [u'7kv903nfjfr9', 0, 35]]
Sort:
out.sort(key=lambda x: float(x[1]))
Error:
TypeError: float() argument must be a string or a number
What is the issue here?
Works perfectly for me... Your real list will most probably include an item where the second element is not convertible to a float, eg:
>>> out = [[u'test', None, 35], [u'7kv903nfjfr9', 0, 35], [u'm3u33mm534o', 14, 23], [u'2w3dfbv333g', 20, 34]]
>>> x = out.sort(key=lambda x: float(x[1]))
...
TypeError: float() argument must be a string or a number
To debug, just do something like
for i in out:
try:
float(i[1])
except TypeError:
print "error is here:", i
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With