python - 查找一个数组大于第二个数组中的元素的索引
问题描述
我有两个数组
a = np.array([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16])
b = np.array([0,5,10,15])
我想要一个输出数组,其长度为b
每个元素b[i]
的第一个元素的索引,其中的索引a
至少为b[i]
:
out = np.array([0, 5, 10, 15]
一个缓慢的解决方案是:
out = []
for x in b:
i = np.argmax( a >= x )
out.append( i )
这是边际速度增加:
out = []
i=0
for x in b:
i = np.argmax( a[i:] >= x ) + i
out.append( i )
关于纯 numpy 解决方案的任何想法?这是非常缓慢的。谢谢
解决方案
如果a
已排序,则可以使用a.searchsorted(b)
.
推荐阅读
- python - 如何自动对每小时范围内的数据求和?
- itext - IText 7 中的多行文本拟合
- database - 有效过滤数据库中的数据
- javascript - 我正在尝试安装 react 但我不断收到“错误:EPERM:不允许操作”
- nftables - nftables:覆盖默认配置文件
- opencv - 读取 OpenCV FOURCC 代码 int 到 char 转换
- javascript - 在移动浏览器中使用 go 键跳过 HTML“日期”输入
- python - 在 Python 中对 2 个数组进行 t 测试时获取数组而不是数字
- function - 参数类型“Function”不能分配给参数类型“void Function(bool?)?”。空对象
- javascript - JavaScript 中模块的执行上下文是什么?如何在浏览器的控制台中显式访问它?