Python find element in 2d array

I need to figure out how I can find all the index of a value in a 2d numpy array.

For example, I have the following 2d array:

[[[1 1 0 0],
  [0 0 1 1],
  [0 0 0 0]]]

I need to find the index of all the 1's and 0's.

1: [[0, 0], [0, 1], [1, 2], [1, 3]]
0: [[0, 2], [0, 3], [1, 0], [1, 1], [the entire all row]]

I tried this but it doesn't give me all the indexes:

t = [[index, row.index[1]] for index, row in enumerate[x] if 1 in row]

Basically, it gives me only one of the index in each row [[0, 0], [1, 2]].

Two dimensional array is an array within an array. It is an array of arrays. In this type of array the position of an data element is referred by two indices instead of one. So it represents a table with rows an dcolumns of data.

In the below example of a two dimensional array, observer that each array element itself is also an array.

Consider the example of recording temperatures 4 times a day, every day. Some times the recording instrument may be faulty and we fail to record data. Such data for 4 days can be presented as a two dimensional array as below.

Day 1 - 11 12 5 2 
Day 2 - 15 6 10 
Day 3 - 10 8 12 5 
Day 4 - 12 15 8 6 

The above data can be represented as a two dimensional array as below.

T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]

Accessing Values

The data elements in two dimesnional arrays can be accessed using two indices. One index referring to the main or parent array and another index referring to the position of the data element in the inner array.If we mention only one index then the entire inner array is printed for that index position.

Example

The example below illustrates how it works.

from array import *

T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]

print[T[0]]

print[T[1][2]]

Output

When the above code is executed, it produces the following result −

[11, 12, 5, 2]
10

To print out the entire two dimensional array we can use python for loop as shown below. We use end of line to print out the values in different rows.

Example

from array import *

T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]
for r in T:
   for c in r:
      print[c,end = " "]
   print[]

Output

When the above code is executed, it produces the following result −

11 12  5 2 
15  6 10 
10  8 12 5 
12 15  8 6 

Inserting Values

We can insert new data elements at specific position by using the insert[] method and specifying the index.

Example

In the below example a new data element is inserted at index position 2.

from array import *
T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]

T.insert[2, [0,5,11,13,6]]

for r in T:
   for c in r:
      print[c,end = " "]
   print[]

Output

When the above code is executed, it produces the following result −

11 12  5  2 
15  6 10 
 0  5 11 13 6 
10  8 12  5 
12 15  8  6 

Updating Values

We can update the entire inner array or some specific data elements of the inner array by reassigning the values using the array index.

Example

from array import *

T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]

T[2] = [11,9]
T[0][3] = 7
for r in T:
   for c in r:
      print[c,end = " "]
   print[]

Output

When the above code is executed, it produces the following result −

11 12 5  7 
15  6 10 
11  9 
12 15 8  6 

Deleting the Values

We can delete the entire inner array or some specific data elements of the inner array by reassigning the values using the del[] method with index. But in case you need to remove specific data elements in one of the inner arrays, then use the update process described above.

Example

from array import *
T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]

del T[3]

for r in T:
   for c in r:
      print[c,end = " "]
   print[]

Output

When the above code is executed, it produces the following result −

11 12 5 2 
15 6 10 
10 8 12 5 

Array is a data structure used to store elements. An array can only store similar types of elements. A Two Dimensional is defined as an Array inside the Array. The index of the array starts with 0 and ends with a size of array minus 1. We can create ‘n’ number of arrays in an array.

In the above image, we can see that an index uniquely identifies each array element.

In this Python List tutorial, you will learn:

  • How to Create Array in Python?
  • Accessing the values
  • Inserting the values into the two-dimensional array
  • Updating the values into the two-dimensional array
  • Deleting the values from two-dimensional array
  • Get the size of two-dimensional array

How to Create Array in Python?

We can create a two-dimensional array[list] with rows and columns.

Syntax:

[[r1,r2,r3,..,rn],[c1,c2,c3,.......,cn]]

Where,

r stands for rows and c stands for columns

Example: Following is the example for creating

2D array with 4 rows and 5 columns

array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]]
#display
print[array]

Output:

[[23, 45, 43, 23, 45], [45, 67, 54, 32, 45], [89, 90, 87, 65, 44], [23, 45, 67, 32, 10]]

Accessing the values

We can access the values using index position

Syntax:

We can get row value using [] operator

array[row index]

We can get column value using [][]

Array[row index][column index]

where,

  • array is an input array
  •  row index is the row index position starts from 0
  •  column index is the column index position starts from 0 in a row.

Example:

In this example we are going to access the values using index positions

#creare 2D array with 4 rows and 5 columns
array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]]

#display
print[array]

#get the first row
print[array[0]]

#get the third row
print[array[2]]

#get the first row third element
print[array[0][2]]

#get the third row forth element
print[array[2][3]]

Output:

[[23, 45, 43, 23, 45], [45, 67, 54, 32, 45], [89, 90, 87, 65, 44], [23, 45, 67, 32, 10]]
[23, 45, 43, 23, 45]
[89, 90, 87, 65, 44]
43
65

We can also access elements using for loop

Syntax:

for rows in the array:
  for columns in rows:
    print[columns]

where,

  • rows are used to iterate the row by row
  • columns is used to iterate the values present in each row.

Example:

Creare 2D array with 4 rows and 5 columns
array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]]
#use for loop to iterate the array
for rows in array:
 for columns in rows:
   print[columns,end=" "]
   print[]

Output:

23 45 43 23 45
45 67 54 32 45
89 90 87 65 44
23 45 67 32 10

Inserting the values into the two-dimensional array

Here we are going to insert values into two dimensional array using insert[] function

Syntax:

array.insert[index,[values]]

where,

  • the array is the input array
  • the index is the row position to insert a particular row
  • value are the values to be inserted into the array

Example: Insert to values in the array

#creare 2D array with 4 rows and 5 columns
array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]]

#insert the row at 5 th position
array.insert[2, [1,2,3,4,5]]

#insert the row at 6 th position
array.insert[2, [1,2,3,4,5]]

#insert the row at 7 th position
array.insert[2, [1,2,3,4,5]]

#display
print[array]

Output:

[[23, 45, 43, 23, 45], [45, 67, 54, 32, 45], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [89, 90, 87, 65, 44], [23, 45, 67, 32, 10]]

Updating the values into the two-dimensional array

Here are two methods for updating values in the 2-D array[list].

You can update rows by using the following syntax

array[row_index]= [values]

You can update column values inside rows by using the following syntax

array[row_index][column_index]= [values]

Example:

#creare 2D array with 4 rows and 5 columns
array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]]

#update row values in the 3rd row
array[2]=[0,3,5,6,7]

#update row values in the 5th row
array[2]=[0,3,5,6,7]

#update the first row , third column
array[0][2]=100

#update the second row , third column
array[1][2]=400

#display
print[array]

Output:

[[23, 45, 100, 23, 45], [45, 67, 400, 32, 45], [0, 3, 5, 6, 7], [23, 45, 67, 32, 10]]

Deleting the values from two-dimensional array

You can delete rows using the del function

Syntax:

del array[index]

where,

  • the array is the input array
  • index refers to the row index

Example:

#creare 2D array with 4 rows and 5 columns
array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]]

#delete row values in the 3rd row
del array[2]

#delete row values in the 2nd row
del array[1]

#display
print[array]

Output:

[[23, 45, 43, 23, 45], [23, 45, 67, 32, 10]]

Get the size of two-dimensional array

You can get the size of the two-dimensional array using the line[] function. It will return the number of rows in the array

Syntax:

len[array]

Example:

Get the length of the two-dimensional array

#creare 2D array with 4 rows and 5 columns
array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]]
#display
print[len[array]]

Output:

4

Summary:

Here are some important Array[list] Methods

Method Description Syntax Example  
Append[]: This method helps us to add an element at the end to an existing array.   array.append[value]  
# Adding an element using append method to the end of an array array=[1,2,3]      array.append[4]      print[array]    

Output:

[1,2,3,4]
 
Clear[]: This method helps us to remove all the elements present in an array array.clear[]
# Removing all the elements from an array array=[1,2,3] 
array.clear[]

Output:

[]
Copy[]: This method helps us to copy the contents of an array to a new array array1=array.copy[]
#Copying the elements from an array to a new array array=[1,2,3] array1=[] 
array1=array.copy[] 
  print[array1]  

Output:

[1,2,3]
Count[]: This method helps us to count the no.of occurences of a an specified element in an array array.count[element]
#Counting the no of times an element is present in an array array=[1,2,3]
print[array.count[8]]
 Output: 0
Extend[]: This method helps us to extend an array beyond it’s consisting elements. array.extend[array1]
#Extending an existing array with another array array=[1,2,3] array1=[4,5,6] array.extend[array1] print[array]
Output: [1,2,3,4,5,6]
Index[]: This method helps us to find the index of an element in an array array.index[element]
#returing the index an element in an array array=[1,2,3] 
print[array.index[3]]

Output:

2
Insert[]: This method helps us to insert elements into an array at specified index. array.insert[index,element]
#Inserting an element at a specified index into an array array=[1,2,3] 
array.insert[2,4] 
  print[array]

Output:

[1,2,4,3]
Pop[]: This method helps us to remove an element at specified index array.pop[index]
#Removing an element at specified index through pop method
array=[1,2,3] 
array.pop[2] 
  print[array]

Output:

[1,2]
Remove[]:  This method helps us to remove an particular matching element in an array. array.remove[element]
array=[1,2,3]
array.remove[2]
  print[array]

Output:

[1,3]
Reverse[]: This method helps is to reverse the elements in an array. array.reverse[]
array=[1,2,3,4] 
array.reverse[] 
  print[array]

Output:

[4,3,2,1]

How do you search for an element in a two

Solution Step.
Run a nested loop, outer loop for row and inner loop for the column..
Check every element with target and if the element is found then return True..
If the element is not found, then return False..

How do you find the index of an element in a 2D array in python?

“python how to find index of an element in a 2d list” Code Answer's.
myList = [[1, 2], [3, 4], [5, 6]].
def index_2d[myList, v]:.
for i, x in enumerate[myList]:.
if v in x:.
return i, x. index[v].

How do you access the elements of a 2D list in python?

Use list indexing to access elements in a 2D list. Use the list indexing syntax a_2d_list[x][y] to access an element at index y in the nested list at index x .

How do you find the index of a 2D array?

If they are not sorted, you will have to loop through all indexes [using double loop] and check if it is a match. Show activity on this post. This is a primitive array, so it should be directly accessibly with the index: int[] indexValue = arr[88];

Chủ Đề