python numpy在保持行的整体性的情况下按列排序

某公司的数据日常处理。看着规整的数据我就想到了numpy。


这是数据:

>>>> a
array([[ 2,  7,  1,  2],
       [35, 9, 1, 2],
       [22,  12,  4,  2]])
>>> 

 按照第一列排序,想要变成这样的效果: 

>>> a
array([[ 2,  7,  1,  2],
       [22, 12, 4, 2],
       [35,  9,  1,  2]])
>>> 

行的整体性并没有被破坏。


开写:

# -*- coding: cp936 -*-

import numpy as np
import pprint 
a = np.array(
                [[  2,  7,  1, 2],
                [ 35,  9,  1, 2],
                [ 22, 12,  4, 2]]
             )

a1 = a[:,::-1].T
a2 = np.lexsort(a1)
a3 = a[a2]

pprint.pprint(a3)

好吧,就3行,开始写注释吧,不然自己都会忘了(不可能,我这么帅,怎么可能忘记)

a1 = a[:,::-1].T
注释:

#先逆序再转置
#逆序结果:
#[2,1,7,2]
#[2,1,9,35]
#[2,4,12,22]
#转置结果:
#[2,2,2]
#[1,1,4]
#[7,9,12]
#[2,35,22]


a2 = np.lexsort(a1)
注释:

#np.lexsort(),以列为整体性进行排序,重点是:从最后一行开始比较大小,正因为从最后一行开始排序,前面才用逆序
#排序结果:
#[0 2 1]
#0表示,排序后的第1列在a1的第0列,[2,1,7,2]
#2表示,排序后的第2列在a1的第2列,[2,4,12,22]
#1表示,排序后的第3列在a1的第1列,[2,1,9,35]


a3 = a[a2]
注释:
#拿着新的序列号[0 2 1],调整下a就是我们要的结果了



好了,思考下,如果按第二列排序怎么办?

Ps:之前的数据本来排好序了,更换下数据。

开写:

# -*- coding: cp936 -*-

import numpy as np
from numpy import * 
import pprint 
a = np.array(
                [[  2,  77,  1, 2],
                [ 35,  9,  1, 2],
                [ 22, 12,  4, 2]]
             )
print '排序前:'
pprint.pprint(a)

a1 = a.T
a2 = a1[array([0,3,2,1])]  #2列换到最后列(因为转置,此时应该说2行换最后行)
a3 = np.lexsort(a2)
a4 = a[a3]

print '排序后:'
pprint.pprint(a4)
结果:

>>> 
排序前:
array([[ 2, 77,  1,  2],
       [35,  9,  1,  2],
       [22, 12,  4,  2]])
排序后:
array([[35,  9,  1,  2],
       [22, 12,  4,  2],
       [ 2, 77,  1,  2]])
>>> 

其实就是把第2列换到最后一列,然后重复之前的操作。



好吧,思考下,按照任意列排序,怎么搞?

输入参数:col_index(0,1,2,3...)表示按照第col_index列排序。

分析:因为转置的原因,题目要求等价于按照转置后的第col_index行排序。


这个还真有点意思,因为coding的时候,要把第col_index行和最后一行互换,不知道怎么写这个:

if col_index = 0: a2 = a1[array([3,1,2,col_index])]
if col_index = 1: a2 = a1[array([0,3,2,col_index])]
if col_index = 2: a2 = a1[array([0,1,3,col_index])]
if col_index = 3: a2 = a1[array([0,1,2,col_index])]

我可不想用100个if来表示100列。

动态生成?不可能,我肯定避开不了做判断的时候。

算了,既然数据保证了每一行格式都是规整的,直接两行交换吧。numpy做两行运算还是挺快的。

交换a,b :a = a + b, b = a - b, a = a- b。

# -*- coding: cp936 -*-

import numpy as np
from numpy import * 
import pprint 
a = np.array(
                [[  2,  77,  1, 2],
                [ 35,  9,  1, 2],
                [ 22, 12,  4, 2]]
             )
print '排序前:'
pprint.pprint(a)


def sort_by_col(a,col_index):
    a1 = a.T

    col_max = a.shape[-1]-1

    if col_index < col_max:
        #两行互换
        a1[col_index] = a1[col_index] + a1[col_max]
        a1[col_max] = a1[col_index] - a1[col_max]
        a1[col_index] = a1[col_index] - a1[col_max]
        
        a2 = np.lexsort(a1)

        #因为a1的行交换影响了a(虽然id不同,但是是同一片内存),得到序列结果后再换回来,保持a的纯洁
        a1[col_index] = a1[col_index] + a1[col_max]
        a1[col_max] = a1[col_index] - a1[col_max]
        a1[col_index] = a1[col_index] - a1[col_max]
    else:
        a2 = np.lexsort(a1)
    
    return a[a2]



for i in range(4):
    col_index = i
    a = sort_by_col(a,col_index)
    print '当col_index = %d ,排序后:' % col_index
    pprint.pprint(a)
结果:

>>> 
排序前:
array([[ 2, 77,  1,  2],
       [35,  9,  1,  2],
       [22, 12,  4,  2]])
当col_index = 0 ,排序后:
array([[ 2, 77,  1,  2],
       [22, 12,  4,  2],
       [35,  9,  1,  2]])
当col_index = 1 ,排序后:
array([[35,  9,  1,  2],
       [22, 12,  4,  2],
       [ 2, 77,  1,  2]])
当col_index = 2 ,排序后:
array([[35,  9,  1,  2],
       [ 2, 77,  1,  2],
       [22, 12,  4,  2]])
当col_index = 3 ,排序后:
array([[35,  9,  1,  2],
       [ 2, 77,  1,  2],
       [22, 12,  4,  2]])
>>> 








  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值