• 【数据分析&数据挖掘】数组的排序


     1 import numpy as np
     2 
     3 # 创建数组
     4 arr = np.array([[8, 1, 9], [7, 6, 5]])
     5 print("arr: 
    ", arr)
     6 
     7 # sort()直接排序
     8 # 在列的方向上排序
     9 arr.sort(axis=-1)
    10 print("axis=-1排序之后的结果:
    ", arr)
    11 arr.sort(axis=1)
    12 print("axis=1排序之后的结果:
    ", arr)
    13 # 在行的方向上排序
    14 arr.sort(axis=0)
    15 print("axis=0排序之后的结果:
    ", arr)
    16 
    17 # argsort()间接排序
    18 arr = np.array([5, 4, 6, 7, 1])
    19 print("arr:
    ", arr)
    20 # 升序排序之后元素原来所处的下标
    21 res = arr.argsort()
    22 
    23 print("res: 
    ", res)
    24 
    25 # 二维数组
    26 arr = np.array([[3, 9, 1], [0, 8, 5]])
    27 print("arr:
    ", arr)
    28 # 按照列的方向排序
    29 res = arr.argsort(axis=-1)
    30 # 按照行的方向排序
    31 res = arr.argsort(axis=0)
    32 
    33 print("res:
    ", res)
    34 
    35 # lexsort()
    36 a = np.array([3, 2, 6, 4, 5])
    37 b = np.array([50, 30, 40, 20, 10])
    38 c = np.array([400, 300, 600, 100, 200])
    39 
    40 # 返回值为最后一个数组元素排序之后原来所处的下标
    41 res = np.lexsort([a, b, c])
    42 print("res: 
    ", res)
    43 
    44 res_a = [a[i] for i in res]
    45 res_b = [b[i] for i in res]
    46 res_c = [c[i] for i in res]
    47 
    48 print("a按照c的规则排序之后的结果: 
    ", res_a)
    49 print("b按照c的规则排序之后的结果: 
    ", res_b)
    50 print("c按照c的规则排序之后的结果: 
    ", res_c)
  • 相关阅读:
    python 登录与注册
    python 深浅拷贝
    列表|字典|集合推导式
    正则表达式——笔记
    Python thread
    allure报告自定义logo和名称
    回归测试用例编写思路
    测试用例规范【如何编写测试用例】
    git与pycharm的使用详解(git+gitlab+pycham)
    接口自动化
  • 原文地址:https://www.cnblogs.com/Tree0108/p/12115538.html
Copyright © 2020-2023  润新知