Dec-16-2021, 09:30 AM
Hi guys, it's on my first exercise list with Numpy.
That problem:
Consider the 3 arrays below
Return the value of the xarr array if the value is True in the cond array. Otherwise, return the value of the yarr array.
In response I get this:
But I would like to get this:
I'm starting study with Data Science, this is the first list of exercises I've done with NumPy. I was confused because when I use the "prints" that are inside the "if/else", they return the value I want, but when I assign them to the new "array" they don't go as I imagine. What logic am I wrong or missing? from now on, grateful
That problem:
Consider the 3 arrays below
Return the value of the xarr array if the value is True in the cond array. Otherwise, return the value of the yarr array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import numpy as np xarr = np.array([ 1.1 , 1.2 , 1.3 , 1.4 , 1.5 ]) yarr = np.array([ 2.1 , 2.2 , 2.3 , 2.4 , 2.5 ]) cond = np.array([ True , False , True , True , False ]) #my answer to the problem result1 = np.arange(cond.size) a = 0 for i in cond: if i = = True : #print(xarr[a]) result1[a] = xarr[a] a = a + 1 else : #print(yarr[a]) result1[a] = yarr[a] a = a + 1 print (result1) |
1 |
[ 1 2 1 1 2 ] |
1 |
[ 1.1 2.2 1.3 1.4 2.5 ] |