{ "cells": [ { "cell_type": "markdown", "id": "6b60b290", "metadata": {}, "source": [ "# Sortieren\n", "\n", "Wie in Pythons `list` können NumPy-Arrays mit der Sortiermethode [numpy.sort](https://numpy.org/doc/stable/reference/generated/numpy.sort.html) in-place sortiert werden. Dabei könnt ihr jeden eindimensionalen Abschnitt von Werten in einem mehrdimensionalen Array an Ort und Stelle entlang einer Achse sortieren, indem ihr die Achsennummer zum Sortieren übergebt:" ] }, { "cell_type": "code", "execution_count": 1, "id": "426015ee", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-1.14718824, -0.54209799, -0.45049905],\n", " [-0.02451891, 0.41275411, 1.33090796],\n", " [ 0.70262199, 1.38217277, -1.14442492],\n", " [-0.22001925, -0.31659483, 0.36900167],\n", " [-0.19731392, -0.29403516, -0.50274743],\n", " [-0.93386482, -1.12466501, -0.99819188],\n", " [-2.33723639, -1.10837584, 0.00246974]])" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "data = np.random.randn(7, 3)\n", "\n", "data" ] }, { "cell_type": "code", "execution_count": 2, "id": "ce1954e2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-2.33723639, -1.12466501, -1.14442492],\n", " [-1.14718824, -1.10837584, -0.99819188],\n", " [-0.93386482, -0.54209799, -0.50274743],\n", " [-0.22001925, -0.31659483, -0.45049905],\n", " [-0.19731392, -0.29403516, 0.00246974],\n", " [-0.02451891, 0.41275411, 0.36900167],\n", " [ 0.70262199, 1.38217277, 1.33090796]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.sort(0)\n", "\n", "data" ] }, { "cell_type": "markdown", "id": "48e99a1e", "metadata": {}, "source": [ "`np.sort` gibt hingegen eine sortierte Kopie eines Arrays zurück, anstatt das Array an Ort und Stelle zu verändern:" ] }, { "cell_type": "code", "execution_count": 3, "id": "aee5c3d1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-2.33723639, -1.14442492, -1.12466501],\n", " [-1.14718824, -1.10837584, -0.99819188],\n", " [-0.93386482, -0.54209799, -0.50274743],\n", " [-0.45049905, -0.31659483, -0.22001925],\n", " [-0.29403516, -0.19731392, 0.00246974],\n", " [-0.02451891, 0.36900167, 0.41275411],\n", " [ 0.70262199, 1.33090796, 1.38217277]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sort(data, axis=1)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.13 Kernel", "language": "python", "name": "python313" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.0" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }