![]() |
|
[Tut] Python – Return NumPy Array From Function - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python – Return NumPy Array From Function (/thread-100092.html) |
[Tut] Python – Return NumPy Array From Function - xSicKxBot - 10-16-2022 Python – Return NumPy Array From Function 5/5 – (1 vote) Do you need to create a function that returns a NumPy array but you don’t know how? No worries, in sixty seconds, you’ll know! Go! ![]() A Python function can return any object such as a NumPy Array. To return an array, first create the array object within the function body, assign it to a variable Code: arrCode: return arr Recommended Tutorial: How to Initialize a NumPy Array? 6 Easy WaysCreate and Return 1D Array For example, the following code creates a function Code: create_array()Code: np.arange()Code: import numpy as np def create_array(): ''' Function to return array ''' return np.arange(10) numbers = create_array()
print(numbers)
# [0 1 2 3 4 5 6 7 8 9]The Code: np.arange([start,] stop[, step])Code: startCode: stopThe Code: stepCode: np.arange(1, 6, 2)Code: [1, 3, 5]To better understand the function, have a look at this video: I also created this figure to demonstrate how NumPy’s Code: arange()![]() In the code example, we used Code: np.arange(10)Code: start=0Code: step=1Code: stop=10If you need an even deeper understanding, I’d recommend you check out our full guide on the Finxter blog. Recommended Tutorial: NumPy Arange Function — A Helpful Illustrated GuideCreate and Return 2D NumPy Array You can also create a 2D (or multi-dimensional) array in a Python function by first creating a 2D or (xD) nested list and converting the nested list to a NumPy array by passing it into the Code: np.array()The following code snippet uses nested list comprehension to create a 2D NumPy array following a more complicated creation pattern: Code: import numpy as np def create_array(a,b): ''' Function to return array ''' lst = [[(i+j)**2 for i in range(a)] for j in range(b)] return np.array(lst) arr = create_array(4,3)
print(arr)Output: Code: [[ 0 1 4 9] [ 1 4 9 16] [ 4 9 16 25]]I definitely recommend reading the following tutorial to understand nested list comprehension in Python: Recommended Tutorial: Nested List Comprehension in PythonMore Ways There are many other ways to return an array in Python. For example, you can use either of those methods inside the function body to create and initialize a NumPy array:
To get a quick overview what to put into the function and how these methods work, I’d recommend you check out our full tutorial. Recommended Tutorial: How to Initialize a NumPy Array? 6 Easy WaysRelated Tutorials Programmer Humor Code: Q: How do you tell an introverted computer scientist from an extroverted computer scientist? A: An extroverted computer scientist looks at your shoes when he talks to you. |