Python sort by a row or column of a two-dimensional array (NumPy lexsort)

Source: Internet
Author: User

Lexsort supports the ordering of arrays in the order of specified rows or columns; is an indirect sort, lexsort does not modify the original array and returns the index. By default, the last line element has a small to large sort, which returns the position of the last row of elements after the index is sorted. Set array A, return index IND, a can be a 1-D or 2-dimensional array, IND returns a one-dimensional array for a one-dimensional array, A[ind] is the sorted array. For a two-dimensional array, an example is detailed below. Import NumPy as NP >>> Aarray ([[2, 7, 4, 2], [35, 9, 1, 5], [22, 12, 3, 2]])
sort by the last column order>>> A[np.lexsort (A.T)]array ([[22, 12, 3, 2], [2, 7, 4, 2], [35, 9, 1, 5]]) sort by the last column in reverse order>>>a[np.lexsort (-A.T)]array ([[35, 9, 1, 5], [2, 7, 4, 2], [22, 12, 3, 2]]) Sort by first column order>>> A[np.lexsort (A[:,::-1]. T)]array ([[2, 7, 4, 2], [22, 12, 3, 2], [35, 9, 1, 5]]) sort by last line order>>> A.t[np.lexsort (a)]. Tarray ([[2, 4, 7, 2], [5, 1, 9, 35], [2, 3, 12, 22]]) Sort by first row order>>> A.t[np.lexsort (a[::-1,:])]. Tarray ([[2, 2, 4, 7], [5, 35, 1, 9], [2, 22, 3, 12]])

Python sort by a row or column of a two-dimensional array (NumPy lexsort)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.