我试图按照2元组中元素的比例对列表L进行排序。
Parameters
----------
L : {list} of 2-tuples ({tuple}) of {int}
Returns
-------
None
Example
-------
>>> L = [(2, 4), (8, 5), (1, 3), (9, 4), (3, 5)]
>>> sort_by_ratio(L)
>>> L
[(1, 3), (2, 4), (3, 5), (8, 5), (9, 4)]
到目前为止我已经
了L[:] = sorted(L,key = lambda ratio: ratio[0]/ratio[1])
但它以某种方式给了我一个[(2, 4),(1, 3),(3, 5), (8, 5), (9, 4)]
我哪里做错了?
答案 0 :(得分:1)
L = [(2, 4), (8, 5), (1, 3), (9, 4), (3, 5)]
L[:] = sorted(L,key = lambda ratio: 1.*ratio[0]/ratio[1])
print L