在此程序中,我们必须在给定范围内打印numpy数组的元素。和使用的不同numpy函数。numpy.where()numpy.logical_and()
Step 1: Define a numpy array. Step 2: Use np.where() and np.logical_and() to find the numbers within the given range. Step 3: Print the result.
import numpy as np arr = np.array([1,3,5,7,10,2,4,6,8,10,36]) print("Original Array:\n",arr) result = np.where(np.logical_and(arr>=4, arr<=20)) print(result)输出结果
Original Array: [ 1 3 5 7 10 2 4 6 8 10 36] (array([2, 3, 4, 6, 7, 8, 9], dtype=int64),)
结果给出满足功能条件的元素的索引位置。np.where()