import numpy as np
# 创建一个示例数组
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# 使用 np.where 找到所有大于 5 的元素的索引
result = np.where(arr > 5)
print("大于 5 的元素的索引:", result)
# 解释:
# np.where(condition) 返回满足条件的元素的索引。
# 在这个例子中,condition 是 arr > 5,因此它会返回所有大于 5 的元素的索引。
# 另一个示例:使用 np.where 进行元素替换
new_arr = np.where(arr > 5, arr * 2, arr)
print("将大于 5 的元素乘以 2:", new_arr)
# 解释:
# np.where(condition, x, y) 返回一个数组,其中满足 condition 的元素取自 x,否则取自 y。
# 在这个例子中,condition 是 arr > 5,x 是 arr * 2,y 是 arr。
# 因此,大于 5 的元素会被乘以 2,其他元素保持不变。
上一篇:python dict pop
下一篇:python docstring
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站