python - 如果数组中的值匹配条件,则用另一个数组中相同位置的值替换数组中的值的最快方法
问题描述
我正在尝试使用此语法将数组中的值替换为另一个数组中相同位置的值(如果它们匹配条件):
array[array>limit]=other_array[array>limit]
它有效,但我想我可能会很难做到。有什么想法吗?
解决方案
使用np.where
:
参数
条件:array_like,布尔
Where True, yield x, otherwise yield y.
x,y:array_like
Values from which to choose. x, y and condition need to be broadcastable to some shape.
退货
出:ndarray
An array with elements from x where condition is True, and elements from y elsewhere.
例子:
a1 = np.array([3, 2, 4, 1])
a2 = a1 + 10
limit = 2
>>> np.where(a1 > limit, a2, a1)
array([13, 2, 14, 1])
推荐阅读
- apache - Hive - 捕获行条目字节的平均大小
- javascript - 时刻时区 js 返回错误的日期时间
- python - 如何用 Python Pandas 替换数据库的部分?
- spring-boot - 我尝试了 GET 方法,但发生了 TypeMismatchException
- elasticsearch - 在 Elasticsearch 中搜索/匹配多个字段
- flutter - 根据flutter firebase中的特定条件杀死所有通过互联网运行的进程
- python - 使用自定义条件删除 Dataframe 中的行
- wordpress - woocommerce 获取自定义支付数据设置
- c# - 无法打印到包含 ZPL (Zebra) 的打印机
- android - Kotlin java.lang.NullPointerException: view.findViewById(R.id.textview) 不能为空