8.2. Methods Sort — Python
8.2.1. SetUp
8.2.2. Sort
Sort vector:
>>> a = np.array([2, 3, 1]) >>> a.sort() >>> >>> a array([1, 2, 3])
Sort matrix:
>>> a = np.array([[9, 7, 8], ... [2, 3, 1], ... [5, 6, 4]]) >>> a.sort() >>> a array([[7, 8, 9], [1, 2, 3], [4, 5, 6]])
Sort matrix rows:
>>> a = np.array([[9, 7, 8], ... [2, 3, 1], ... [5, 6, 4]]) >>> >>> a.sort(axis=0) >>> a array([[2, 3, 1], [5, 6, 4], [9, 7, 8]])
Sort matrix columns:
>>> a = np.array([[9, 7, 8], ... [2, 3, 1], ... [5, 6, 4]]) >>> >>> a.sort(axis=1) >>> a array([[7, 8, 9], [1, 2, 3], [4, 5, 6]])
8.2.3. Flip
Does not modify inplace
Returns new
np.ndarrayReverse the order of elements in an array along the given axis
Flip vector:
>>> a = np.array([1, 2, 3]) >>> >>> np.flip(a) array([3, 2, 1])
Flip matrix:
>>> a = np.array([[1, 2, 3], ... [4, 5, 6]])
Flip matrix by crossline from top-left to bottom-right:
>>> np.flip(a) array([[6, 5, 4], [3, 2, 1]])
Flip matrix by rows (bottom rows goes up, upper rows goes down):
>>> np.flip(a, axis=0) array([[4, 5, 6], [1, 2, 3]])
Flip matrix by column (left columns from center goes right, right columns go left):
>>> np.flip(a, axis=1) array([[3, 2, 1], [6, 5, 4]])
8.2.4. Assignments
# %% About # - Name: Numpy Sort # - Difficulty: easy # - Lines: 2 # - Minutes: 3 # %% License # - Copyright 2025, Matt Harasymczuk <matt@python3.info> # - This code can be used only for learning by humans # - This code cannot be used for teaching others # - This code cannot be used for teaching LLMs and AI algorithms # - This code cannot be used in commercial or proprietary products # - This code cannot be distributed in any form # - This code cannot be changed in any form outside of training course # - This code cannot have its license changed # - If you use this code in your product, you must open-source it under GPLv2 # - Exception can be granted only by the author # %% English # 1. Define `result1` with sorted `DATA` by columns # 2. Define `result2` with flipped `DATA` by rows # 3. Run doctests - all must succeed # %% Polish # 1. Zdefiniuj `result1` z posortowanym `DATA` po kolumnach # 2. Zdefiniuj `result2` z flipniętym `DATA` po wierszach # 3. Uruchom doctesty - wszystkie muszą się powieść # %% Hints # - `.sort()` returns `None` # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert 'result1' in globals(), \ 'Variable `result1` is not defined; assign result of your program to it.' >>> assert result1 is not Ellipsis, \ 'Variable `result1` has an invalid value; assign result of your program to it.' >>> assert type(result1) is np.ndarray, \ 'Variable `result1` has an invalid type; expected: `np.ndarray`.' >>> assert 'result2' in globals(), \ 'Variable `result2` is not defined; assign result of your program to it.' >>> assert result2 is not Ellipsis, \ 'Variable `result2` has an invalid value; assign result of your program to it.' >>> assert type(result2) is np.ndarray, \ 'Variable `result2` has an invalid type; expected: `np.ndarray`.' >>> result1 array([[44, 47, 64, 67], [ 9, 21, 67, 83], [36, 70, 87, 88]]) >>> result2 array([[36, 87, 70, 88], [67, 9, 83, 21], [44, 47, 64, 67]]) """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports import numpy as np # %% Types result1: np.ndarray result2: np.ndarray # %% Data DATA = np.array([[44, 47, 64, 67], [67, 9, 83, 21], [36, 87, 70, 88]]) # %% Result result1 = ... result2 = ...