{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Function to compute the range of a list of values" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def my_list_range(values):\n", " \"\"\"\n", " Compute the range of a list of values.\n", " @param values the list of values.\n", " @return the range\n", " \"\"\"\n", " \n", " return max(values) - min(values)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random\n", "x_list = [int(random.uniform(0, 25)) for _ in range(0, 100)]\n", "x_range = my_list_range(x_list)\n", "\n", "print(f'min(x_list) = {min(x_list)}')\n", "print(f'max(x_list) = {max(x_list)}')\n", "print(f'my_list_range(x_list) = {x_range}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Function to compute the range of a list or an array of values" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "def my_array_range(values):\n", " \"\"\"\n", " Compute the range of a list or array of values.\n", " @param values the list or array of values.\n", " @return the range\n", " \"\"\"\n", "\n", " return np.amax(values) - np.amin(values)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np_list_range = my_array_range(x_list)\n", "\n", "print(f'np.amin(x_list) = {np.amin(x_list)}')\n", "print(f'np.amax(x_list) = {np.amax(x_list)}')\n", "print(f'my_array_range(x_list) = {np_list_range}')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x_array = np.array(x_list)\n", "np_array_range = my_array_range(x_array)\n", "\n", "print(f'np.amin(x_array) = {np.amin(x_array)}')\n", "print(f'np.amax(x_array) = {np.amax(x_array)}')\n", "print(f'my_array_range(x_array) = {np_array_range}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2-d array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "matrix = np.array([[2, 5, 6, 3],\n", " [1, 7, 0, 4],\n", " [3, 1, 2, 6]])\n", "\n", "min_of_all = np.amin(matrix)\n", "min_of_cols = np.amin(matrix, axis=0)\n", "max_of_rows = np.amax(matrix, axis=1)\n", "\n", "print(f'np.amin(matrix [all] = {min_of_all}')\n", "print(f'np.amin(matrix, axis=0) [cols] = {min_of_cols}')\n", "print(f'np.amax(matrix, axis=1) [rows] = {max_of_rows}')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.7.7" } }, "nbformat": 4, "nbformat_minor": 4 }