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.
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.
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)In response I get this:
[1 2 1 1 2]But I would like to get this:
[1.1 2.2 1.3 1.4 2.5]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