Home  >  Article  >  Backend Development  >  Python program to find the index of the first occurrence of a specified item in an array

Python program to find the index of the first occurrence of a specified item in an array

王林
王林forward
2023-09-02 23:41:061080browse

Python program to find the index of the first occurrence of a specified item in an array

An array is a data structure used to store elements of the same data type in sequence. And the stored elements are identified by index values. Python has no specific data structure to represent arrays. However, we can use List data structure or Numpy module to handle arrays.

In this article, we have seen various methods of getting the index of the first occurrence of a specified item in an array.

Input and output scenarios

Now let's look at some input and output scenarios.

Suppose we have an input array containing very few elements. In the output, we will get the index of the first occurrence of the specified value.

Input array:
[1, 3, 9, 4, 1, 7]
specified value = 9
Output:
2

The specified element 9 appears only once in the array, and the resulting index of this value is 2.

Input array:
[1, 3, 6, 2, 4, 6]
specified value = 6
Output:
2

The given element 6 appears twice in the array, and the index value of the first occurrence is 2.

Use list.index() method

list.index() method helps you find the index of the first occurrence of a given element in an array. If there is a duplicate element in the list, the first index of that element is returned. The following is the syntax -

list.index(element, start, end)

The first parameter is the element we want to get the index of, the second and third parameters are optional parameters, where to start and end the search for the given element.

The list.index() method returns an integer value which is the index of the given element that we passed to the method.

Example

In the above example, we will use the index() method.

# creating array
arr = [1, 3, 6, 2, 4, 6]
print ("The original array is: ", arr) 
print() 

specified_item = 6

# Get index of the first occurrence of the specified item
item_index = arr.index(specified_item)

print('The index of the first occurrence of the specified item is:',item_index)

Output

The original array is:  [1, 3, 6, 2, 4, 6]
The index of the first occurrence of the specified item is: 2

The given value 6 appears twice in the array, but the index() method only returns the index of the first occurrence of the value.

Use for loop

Similarly, we can use for loops and if conditions to get the index of the specified item that appears at the first position of the array.

Example

Here, we will use a for loop to iterate over the array elements.

# creating array
arr = [7, 3, 1, 2, 4, 3, 8, 5, 4]
print ("The original array is: ", arr) 
print() 

specified_item = 4
# Get the index of the first occurrence of the specified item
for index in range(len(arr)):
   if arr[index] == specified_item:
      print('The index of the first occurrence of the specified item is:',index)
      break

Output

The original array is:  [7, 3, 1, 2, 4, 3, 8, 5, 4]
The index of the first occurrence of the specified item is: 4

The given value 4 occurs repeatedly in the array, but the above example only returns the index of the first occurrence of the value.

Use numpy.where()

numpy.where() method is used to filter array elements based on given conditions. By using this method we can get the index of a given element. The following is the syntax -

numpy.where(condition, [x, y, ]/)

Example

In this example, we will use the conditional numpy.where() method.

import numpy as np

# creating array
arr = np.array([2, 4, 6, 8, 1, 3, 9, 6])
print("Original array: ", arr)

specified_index = 6

index = np.where(arr == specified_index)
# Get index of the first occurrence of the specified item
print('The index of the first occurrence of the specified item is:',index[0][0])

Output

Original array:  [2 4 6 8 1 3 9 6]
The index of the first occurrence of the specified item is: 2

Condition arr == Specified index Checks the given element in the numpy array and returns an array containing the elements that satisfy the given condition or True. From the resulting array, we can get the index of the first occurrence using index[0][0].

The above is the detailed content of Python program to find the index of the first occurrence of a specified item in an array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete