在numpy矩阵中对字母数字字符串进行排序 [英] Sort alphanumeric strings in numpy matrix

查看:215
本文介绍了在numpy矩阵中对字母数字字符串进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个10列不同类型的矩阵.我根据字母数字列对它们进行了排序:

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')

它做得不好,即

BFT_job10_q0
BFT_job13_q0
BFT_job13_q1
BFT_job1_q0

而不是:

BFT_job1_q0
BFT_job10_q0
BFT_job13_q0
BFT_job13_q1

任何numpy都能做到这一点??谢谢!

Anything numpy could do about it?? Thanks!

推荐答案

排序顺序似乎正确.我建议您检查一下编号:

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]])]

key()将执行以下操作:

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

这篇关于在numpy矩阵中对字母数字字符串进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆