Find the index of a value (with approximation) in Python list -
I have a python list and I want to find an approximation of the value in it.
What have I done so far:
value = 3.5 Myलिस्ट = [1,2,3,4,5,6,7,8,9,10] In the category I (0, Lane (my list) +2): If the value & gt; The list [i] and the price & lt; MyList [i + 1]: If the stomach (Myलिस्ट [i] -value) & lt; abs (MyList [i + 1] - value): myindex = mylist.index (i) Other: myindex = mylist.index (i + 1) break sorted without software So, if I get an index, then it will be right.
Do you know whether there is a build-in method to do this or a library that will do it faster than my code?
Since your list has been sorted, you can use the bisect module function Can be used, such as 2, 3, 4, 5, 6, 7, 8, 9, 10], diplomatic imports from Bicect Print Biscata (MeriList, Value) # 3 MyList = 3.5, This will give you an index in which the element will fit in the list and it will be faster (OLG N) than liner search (O).
Comments
Post a Comment