gpt4 book ai didi

python - 找到数组中上下最接近的值?

转载 作者:行者123 更新时间:2023-12-01 05:32:04 25 4
gpt4 key购买 nike

我有一个数组,其中包含[元素编号、x 坐标、y 坐标、z 坐标、半径(极坐标)、θ(极坐标)]在这个数组中,我需要找到最接近指定数字的两个值,上面的一个和下面的一个。这需要在保存 θ 值的数组的最后一列中找到。

值的范围为 0 - 1.5707 弧度(0 - 90 度),在我们的例子中,我们希望能够选择我们想要的指定数字的数量

number=9 
Anglestep = math.pi/2 / number
Anglerange = np.arange(0,math.pi/2+anglestep,anglestep) #math.pi/2+anglestep so that we get math.pi/2 in the array

举个例子,我需要找到指定值上方和下方的两个值:“0.17”

[...['4549', '4.2158604', '49.4799309', '0.0833661', 49.65920902290997, 0.0849981532744405],
['4535', '4.2867651', '49.4913025', '0.0813997', 49.67660795755971, 0.08640089283783374],
['4537', '5.6042995', '49.4534569', '0.0811241', 49.7699967073121, 0.11284330708918186],
['4538', '6.2840257', '49.4676971', '0.0809942', 49.86523874780516, 0.12635612935285648],
['4539', '6.9654546', '49.4909363', '0.0814121', 49.97869879894153, 0.13982362821749783],
['4540', '7.6476088', '49.5210190', '0.0813955', 50.10805567128103, 0.1532211602749019],
['4541', '8.3298655', '49.5605049', '0.0812513', 50.25564948531672, 0.16651831290560243],
['4542', '9.0141211', '49.6065178', '0.0811457', 50.41885547537927, 0.17975113416156624],
['4529', '9.3985014', '49.6320610', '0.0812080', 50.51409018950577, 0.18714756393388338],
['4531', '10.3884563', '49.7157669', '0.0812043', 50.78954127329902, 0.2059930152826599]..]

所以在这种情况下我想要的输出是两个值:(0.16651831290560243, 0.17975113416156624)

最佳答案

In [30]: np.max(arr[arr < .17])
Out[30]: 0.16651831290560243

In [31]: np.min(arr[arr > .17])
Out[31]: 0.17975113416156624

关于python - 找到数组中上下最接近的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19974945/

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