I am trying to a function where it takes in the 3 arrays as reference and then makes a 2 dimensional array with the number of rows as the original array for arr, arr2, arr3
and then it prints the formatted array with max
and min
of it. How can I modify the code below so taht i could get the Expected Output below?
Code:
import numpy as np
def run(arr, arr2, arr3):
rep = np.array([val[ None ].repeat(len(val), 0) for val in [arr, arr2, arr3]], dtype=object)
printer= [[print(iterator[count:]), max(iterator[count:]), min(iterator[count:])]for count,iterator in enumerate(row)] for row in arr]
run(np.array([12,12,-3,-1,2,1]), np.array([-3,-1,-2,1]), np.array([12,-12]))
Expected Output:
[12, 12, -3, -1, 2, 1] max: 12 min: -3
[12, -3, -1, 2, 1] max: 12 min:-3
[-3, -1, 2, 1] max: 2 min: -3
[-1, 2, 1] max: 2 min: -1
[2, 1] max: 2 min: 1
[1] max: 1 min: 1
[-3, -1, -2, 1] max: 1 min: -3
[-1, -2, 1] max: 1 min: -2
[-2, 1] max: 1 min: -2
[1] max: 1 min: 1
[ 12, -12] max: 12 min: -12
[-12] max: -12 min: -12
CodePudding user response:
If you're sure you only need three
def run(arr, arr2, arr3):
[print(arr[i:], f"min={arr[i:].min()}", f"min={arr[i:].max()}") for i in range(len(arr))]
[print(arr2[i:], f"min={arr2[i:].min()}", f"min={arr2[i:].max()}") for i in range(len(arr2))]
[print(arr3[i:], f"min={arr3[i:].min()}", f"min={arr3[i:].max()}") for i in range(len(arr3))]
run(np.array([12,12,-3,-1,2,1]), np.array([-3,-1,-2,1]), np.array([12,-12]))
Otherwise
def run(*args):
for arr in args:
[print(arr[i:], f"min={arr[i:].min()}", f"min={arr[i:].max()}") for i in range(len(arr))]
CodePudding user response:
To start off I think it would be a good idea for you to write down a basic overview of what you want the function to do. For your problem it appears to boil down to 5 steps.
- Get max and min for the array
- Print the array, max, min
- Remove the first col (Assuming this is only taking in a 1d array such as [1, 2, 3, 4])
- Loop will number of columns == 0
- Repeat for all 3 arrays
Btw a 2 dimensional array is an array that looks like:
[ [1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4] ]
So I am assuming that you are meaning you want an 1 dimensional array but correct me if i'm wrong.
import numpy as np
def run(arr, arr2, arr3):
for i in range(arr.size): # Loop through array in the range of its size
max, min = np.amax(arr), np.amin(arr) # Get the max and min value of array
print(arr, " max:", max, " min:", min) # Print array, max, min
arr = np.delete(arr, 0) # Remove fist collom of array
for i in range(arr2.size): # Repeat code for arr2
max, min = np.amax(arr2), np.amin(arr2)
print(arr2, " max:", max, " min:", min)
arr2 = np.delete(arr2, 0)
for i in range(arr3.size): # Repear code for arr3
max, min = np.amax(arr3), np.amin(arr3)
print(arr3, " max:", max, " min:", min)
arr3 = np.delete(arr3, 0)
I would personally recommend you brake down your code it to easier lines. This is to make it more readable and just so it works because most of the time, when you write code like you did it makes it next to impossible to read and fix (also makes it run slower most of the time). Also another thing is that you are passing through 3 arrays but doing the same thing for each so i would recommend just calling the function 3 times instead as shown below.
import numpy as np
def run(arr):
for i in range(arr.size): # Loop through array in the range of its size
max, min = np.amax(arr), np.amin(arr) # Get the max and min value of array
print(arr, " max:", max, " min:", min) # Print array, max, min
arr = np.delete(arr, 0) # Remove fist collom of array
run(arr)
run(arr2)
run(arr3)
Another way you could go about this though is the args way where you look through every argument you pass in as shown.
import numpy as np
def run(*args):
for arr in args: # Loop through all arguments passed
for i in range(arr.size): # Loop through array in the range of its size
max, min = np.amax(arr), np.amin(arr) # Get the max and min value of array
print(arr, " max:", max, " min:", min) # Print array, max, min
arr = np.delete(arr, 0) # Remove fist collom of array