{ "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": { "execution": { "iopub.execute_input": "2026-05-21T16:58:32.238384Z", "iopub.status.busy": "2026-05-21T16:58:32.237990Z", "iopub.status.idle": "2026-05-21T16:58:32.280439Z", "shell.execute_reply": "2026-05-21T16:58:32.280124Z", "shell.execute_reply.started": "2026-05-21T16:58:32.238365Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.48434885, 0.58017775, 0.87659136],\n", " [0.46718992, 0.13668531, 0.65802304],\n", " [0.68315702, 0.47490695, 0.75162819],\n", " [0.01743604, 0.55087254, 0.47620903],\n", " [0.19326966, 0.54774147, 0.89596431],\n", " [0.06853136, 0.07731341, 0.6533622 ],\n", " [0.29960324, 0.10849735, 0.92689653]])" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "\n", "rng = np.random.default_rng()\n", "data = rng.random((7, 3))\n", "\n", "data" ] }, { "cell_type": "code", "execution_count": 2, "id": "ce1954e2", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:58:32.280951Z", "iopub.status.busy": "2026-05-21T16:58:32.280831Z", "iopub.status.idle": "2026-05-21T16:58:32.283533Z", "shell.execute_reply": "2026-05-21T16:58:32.283264Z", "shell.execute_reply.started": "2026-05-21T16:58:32.280943Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.01743604, 0.07731341, 0.47620903],\n", " [0.06853136, 0.10849735, 0.6533622 ],\n", " [0.19326966, 0.13668531, 0.65802304],\n", " [0.29960324, 0.47490695, 0.75162819],\n", " [0.46718992, 0.54774147, 0.87659136],\n", " [0.48434885, 0.55087254, 0.89596431],\n", " [0.68315702, 0.58017775, 0.92689653]])" ] }, "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": { "execution": { "iopub.execute_input": "2026-05-21T16:58:32.283921Z", "iopub.status.busy": "2026-05-21T16:58:32.283844Z", "iopub.status.idle": "2026-05-21T16:58:32.286261Z", "shell.execute_reply": "2026-05-21T16:58:32.285962Z", "shell.execute_reply.started": "2026-05-21T16:58:32.283915Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.01743604, 0.07731341, 0.47620903],\n", " [0.06853136, 0.10849735, 0.6533622 ],\n", " [0.13668531, 0.19326966, 0.65802304],\n", " [0.29960324, 0.47490695, 0.75162819],\n", " [0.46718992, 0.54774147, 0.87659136],\n", " [0.48434885, 0.55087254, 0.89596431],\n", " [0.58017775, 0.68315702, 0.92689653]])" ] }, "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 }