I want to sort this list from lowest to highest value of whole[i][0] / whole[i][1]
before -> whole = [[60, 20], [100, 50], [120, 30]]
after -> whole = [[100,50],[60,20],[120,30]]
I want to sort this list from lowest to highest value of whole[i][0] / whole[i][1]
before -> whole = [[60, 20], [100, 50], [120, 30]]
after -> whole = [[100,50],[60,20],[120,30]]
You may use list.sort()
along with lambda expression as:
>>> whole = [[60, 20], [100, 50], [120, 30]]
>>> whole.sort(key=lambda x: x[0]/float(x[1]))
# get the resultant from div as a `float` ^ in Python 2
# However you may skip the `float` type-cast in Python 3
Final value hold by whole
list after doing .sort()
will be:
>>> whole
[[100, 50], [60, 20], [120, 30]]
You can use sorted with a lambda:
sorted_list = sorted(whole, lambda x: x[0]/float(x[1]))