10 examples of 'find index of element in array python' in Python

Every line of 'find index of element in array python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
62def find_index(self, e):
63 for i in range(self._size):
64 if self._data[i] == e:
65 return i
66 return -1
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

4def find(arr, elem):
5 lo = 0
6 hi = len(arr) - 1
7 while lo < hi:
8 mid = (hi + lo) // 2
9 first = arr[lo]
10 middle = arr[mid]
11 last = arr[hi]
12
13 if first <= middle and elem >= first and elem <= middle:
14 return binary_search(arr, elem, lo, mid)
15 elif last >= middle and elem >= middle and elem <= last:
16 return binary_search(arr, elem, mid+1, hi)
17 elif elem >= first and elem <= middle:
18 hi = mid
19 else:
20 lo = mid + 1
21
22 if arr[lo] == elem:
23 return lo
24 return None
220def getInd(value, array):
221 """Return closest index (rounded down) of a value from sorted array."""
222 ind = np.abs(array-value).argmin()
223 return int(ind)
17def at_i(array, i=0):
18 return [a[i] for a in array]
519@staticmethod
520def FindIndex(array,*__args):
521 """
522 FindIndex[T](array: Array[T],startIndex: int,count: int,match: Predicate[T]) -> int
523
524 FindIndex[T](array: Array[T],startIndex: int,match: Predicate[T]) -> int
525
526 FindIndex[T](array: Array[T],match: Predicate[T]) -> int
527 """
528 pass
59def list_find(l, element):
60 for i, e in enumerate(l):
61 if e == element:
62 return i
63 return None
135def argmin(arr):
136 return arr.index(min(arr))
4def binary_search(array, element):
5 """
6 Perform Binary Search by Iterative Method.
7
8 :param array: Iterable of elements
9 :param element: element to search
10 :return: returns value of index of element (if found) else return None
11 """
12 left = 0
13 right = len(array) - 1
14 while left <= right:
15 mid = (right + left) // 2
16 # indices of a list must be integer
17 if array[mid] == element:
18 return mid
19 elif array[mid] > element:
20 right = mid - 1
21 else:
22 left = mid + 1
23 return None
18def find_missing_element_unsorted_1(arr):
19 max = arr[0]
20 min = arr[1]
21 table = {}
22
23 for i in range(len(arr)):
24 if arr[i] < min:
25 min = arr[i]
26 if arr[i] > max:
27 max = arr[i]
28 table[arr[i]] = True
29
30 for j in range(min, max+1):
31 if j in table:
32 pass
33 else:
34 return j
10def find_index(a, x):
11 'Locate the leftmost value exactly equal to x'
12 i = bisect_left(a, x)
13 if i != len(a) and a[i] == x:
14 return i
15 return -1

Related snippets