- np.where(condition):找到满足条件的数组下标值
# 首先我们定义一个长为12,范围为[1, 100]的随机整数数组。
array = np.random.randint(1, 101, size=12)
# 找到满足数组中元素大于50的array数组下标。
pos = np.where(array > 50)
"""===== output ====
# 得到下标为第2个、3、5、7、9个元素。另外注意np.where返回的是元组类型(即tuple)。
pos = (array([2, 3, 5, 7, 9]),)
"""
# 输出一下array这些下标的值
print(array[pos])
"""===== output ====
array([89, 89, 81, 59, 85])
"""
本文介绍了如何使用NumPy库中的np.where函数来查找数组中满足特定条件的元素及其下标。通过一个具体实例展示了如何生成一个随机整数数组,并找出其中大于50的元素及其对应的下标。
1779

被折叠的 条评论
为什么被折叠?



