Python 排列组合以及多维数组排序

import itertools

# 数组元素的排列与组合
print list(itertools.permutations([1,2,3],2)) #排列
print list(itertools.combinations([1,2,3],2)) # 组合

"""
sort 2D numpy array ordered by first column (x) , then by second column (y) if and only if x is the same.
data = np.array([[0,0,1],
        [0,1,2],
        [0,2,1],
        [1,1,3],
        [1,0,1],
        [1,2,2]])
        [[0 0 1]
np.lexsort((data[:,1],data[:,0]))
output: [0 1 2]
        [0 2 1]
        [1 1 3]
        [1 0 1]
        [1 2 2]]

"""
#  实现对二维数组的排序,排序规则为以前俩列为准,第一列优先,第二列其次
import numpy as np
data = np.array([[0,0,1],
            [0,1,2],
            [0,2,1],
            [1,1,3],
            [1,0,1],
            [1,2,2]])
ind = np.lexsort((data[:,1],data[:,0]))
print data[ind]
#  以数组 a  的从小到大的顺序为基准,对数组 b 进行重排序,并返回排序结果的索引数组
a = np.array([0,1,3,2,6,4,5])
b = np.array([0,1,2,3,4,5,6])
index = np.lexsort((b, a))

 

    原文作者:python入门
    原文地址: https://my.oschina.net/mzzyk/blog/703795
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞