gpt4 book ai didi

python - 使用 np.where 遍历多个数组

转载 作者:太空狗 更新时间:2023-10-30 02:06:57 25 4
gpt4 key购买 nike

我有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com