如何利用 Numpy 列印給定範圍內的陣列元素?
在這個程式中,我們必須列印給定範圍內的 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() 函式中條件的元素的索引位置。
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言
C++
C#
MongoDB
MySQL
Javascript
PHP