Sort alphanumeric strings in numpy matrix

☆樱花仙子☆ 提交于 2019-12-11 08:56:37

问题


I have a matrix with 10 columns of different types. I sorted them based on the alphanumeric column with :

data = np.sort(data, axis=0,order='AlphaNumColumn')

It didn't do the job right, i.e.

BFT_job10_q0
BFT_job13_q0
BFT_job13_q1
BFT_job1_q0

instead of :

BFT_job1_q0
BFT_job10_q0
BFT_job13_q0
BFT_job13_q1

Anything numpy could do about it?? Thanks!


回答1:


The sorting order seems to be right. I would recommend you to review your numbering:

1 becomes 01

If you have to keep your numbering, you can also do:

key = lambda x: '.'.join(x.split('_')[1:3]).replace('job','').replace('q','')

a[np.argsort([float(key(i)) for i in a[:,0]])]

Where key() will do the following:

key('BFT_job10_q0') --> 10.
key('BFT_job1_q0')  --> 1.
key('BFT_job13_q1') --> 13.1
key('BFT_job13_q0') --> 13.


来源:https://stackoverflow.com/questions/17376700/sort-alphanumeric-strings-in-numpy-matrix

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