Numeric sort on two columns but in different order using Python

浪子不回头ぞ 提交于 2019-12-14 04:27:15

问题


I have to sort a multi-column file based on two columns. Both the columns have floating point numbers and first sort should from lower to higher and second on higher to lower. Here is sample file:

A        B        C        D
AK       0.01     200.8    NY
DK       0.90     50.5     PHL
AB       0.0002   750.04   Port
GA       0.076    340.00   NY

So, I have to sort on column B first in order low to high and then on column C in order high to low. The code I have is taking a lot of time and make my laptop unresponsive which I believe should not be the case. Moreover, I do not how can I sort column B in 'reverse' i.e. High to low. Here is the code:

fh_in = open(res_file,'r')
res = [line.strip('\n').split('\t') for line in fh_in]##Line converted to list and read
res_list = list(res) ##List to hold results while pre-processing
res_list.sort(key= lambda x: (float(x[int(1)]),-float(x[2])))##Sort on Col A and B
print ('Sorted results:\n',res_list)

How can I sort on both columns with column B sorted from High to low? What would be the fastest way to achieve so, as I have multiple files and each file has 25,000 rows*50 columns?

Would really appreciate your help.

-AK-


回答1:


Simply return the negative number:

res_list.sort(key=lambda x: (float(x[1]), -float(x[2])))

This reverses the B sort order, but do note that the data is sorted on column A first.

Note that you can combine the sorting with the previous line in your code by using the sorted() function:

res_list = sorted(res, key=lambda x: (float(x[1]), -float(x[2]))


来源:https://stackoverflow.com/questions/17474047/numeric-sort-on-two-columns-but-in-different-order-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!