技术文章和资源

技术文章(时间排序)

热门类别

Python PHP MySQL JDBC Linux

如何使用 Numpy 打印给定范围内的数组元素?

pythonnumpyserver side programmingprogramming

在此程序中,我们必须打印给定范围内的 numpy 数组元素。使用的不同 numpy 函数是 numpy.where() 和 numpy.logical_and()。

算法

步骤 1:定义一个 numpy 数组。
步骤 2:使用 np.where() 和 np.logical_and() 查找给定范围内的数字。
步骤 3:打印结果。

示例代码

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() 函数中条件的元素的索引位置。


相关文章