作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有 2 个列表 distance_boundary 和 distance
distance_boundary = [100,200,300]
distance = [125,255,285,140,160,180]
现在我想创建一个新变量floor,我想根据距离值给floor赋值,它由distance_boundary定义
例如,
distance[0] is 125
因为这是在 distance_boundary[0] 和 distance_boundary[1] 之间所以floor对应的值应该是1
distance[1] is 255
因为这是在 distance_boundary[1] 和 distance_boundary[2] 之间所以floor对应的值应该是2
通过这个循环应该给我 floor 的最终值作为
floor = [1,2,2,1,1,1]
我尝试通过 distance 循环并使用 np.where 进行赋值
floor = []
for j in range(0,len(distance)):
floor = (np.where((distance[j]>distance_boundary[0]) & (distance[j]>distance_boundary[1]))
,1,2)
floor.append(floor)
这没有给我想要的结果,而且它看起来不可扩展。有没有其他更好的解决方案
最佳答案
np.searchsorted
似乎是你要找的东西:
np.searchsorted(distance_boundary, distance)
# array([1, 2, 2, 1, 1, 1])
关于python - 使用 np.where 遍历多个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56660453/
我是一名优秀的程序员,十分优秀!