python:在float数组中找到范围内的值 [英] python: find value within range in float array
本文介绍了python:在float数组中找到范围内的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下的排序python列表,尽管可能会出现多个值:
[0.0943200769115388,0.17380131294164516,0.4063245853719435,
0.45796523225774904,0.5040225609708342,0.5229351852840304,
0.6145136350368882,0.6220712583558284,0.7190096076050408,
0.8486436998476048,0.8957381707345986,0.9774325873910711,
0.9832076130275351,0.985386554764682,1.0]
现在,我想知道数组中某个值可能下降的索引:
例如,0.25的值将落在索引 2
中,因为它在0.173和0.40之间。我想我可以通过列表,并在for循环中执行此操作,但是我想知道是否有更好的方法来做到这一点,也许更有效地计算。我创建这个数组一次,但必须执行很多查找。
解决方案
>> >瓦尔斯= [0.0943200769115388,0.17380131294164516,0.4063245853719435,
0.45796523225774904,0.5040225609708342,0.5229351852840304,
0.6145136350368882,0.6220712583558284,0.7190096076050408,
0.8486436998476048,0.8957381707345986,0.9774325873910711,
0.9832076130275351,0.985386554764682,1.0]
>>> import bisect
>>> bisect.bisect(vals,0.25)
2
I have the following sorted python list, although multiple values can occur:
[0.0943200769115388, 0.17380131294164516, 0.4063245853719435,
0.45796523225774904, 0.5040225609708342, 0.5229351852840304,
0.6145136350368882, 0.6220712583558284, 0.7190096076050408,
0.8486436998476048, 0.8957381707345986, 0.9774325873910711,
0.9832076130275351, 0.985386554764682, 1.0]
Now, I want to know the index in the array where a particular value may fall:
For example, a value of 0.25 would fall in index 2
because it is between 0.173 and 0.40. I guess I can go through the list and do this in a for loop but I was wondering if there is some better way to do this which maybe more computationally efficient. I create this array once but have to perform many lookups.
解决方案
>>> vals = [0.0943200769115388, 0.17380131294164516, 0.4063245853719435,
0.45796523225774904, 0.5040225609708342, 0.5229351852840304,
0.6145136350368882, 0.6220712583558284, 0.7190096076050408,
0.8486436998476048, 0.8957381707345986, 0.9774325873910711,
0.9832076130275351, 0.985386554764682, 1.0]
>>> import bisect
>>> bisect.bisect(vals, 0.25)
2
这篇关于python:在float数组中找到范围内的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文