Multi-dimensional Arrays
In the previous section, we learned about arrays and their operations. Now, let's dive deeper into multi-dimensional arrays.
A multi-dimensional array is an array that contains other arrays. It can be visualized as a matrix with rows and columns. Each element in a multi-dimensional array is identified by its row index and column index.
Creating a 2D Array
To create a 2D array, you can define a list of lists. Each inner list represents a row in the 2D array. For example:
1# Creating a 2D array
2matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]In this example, matrix is a 2D array with 3 rows and 3 columns.
Accessing Elements
You can access elements in a 2D array by using the row and column indices. For example:
1# Accessing elements in a 2D array
2print(matrix[0][1]) # Output: 2In this example, we access the element in the first row and second column.
Modifying Elements
Similar to accessing elements, you can modify elements in a 2D array by specifying the row and column indices. For example:
1# Modifying elements in a 2D array
2matrix[1][2] = 10
3print(matrix) # Output: [[1, 2, 3], [4, 5, 10], [7, 8, 9]]In this example, we modify the element in the second row and third column.
Finding the Length
To find the number of rows in a 2D array, you can use the len() function. To find the number of columns in a specific row, you can use the len() function on that row. For example:
1# Finding the length of a 2D array
2print(len(matrix)) # Output: 3
3print(len(matrix[0])) # Output: 3In this example, we find the length of matrix and the length of the first row.
Adding Elements
You can add a new row to a 2D array by using the append() method. The new row will be added at the end of the 2D array. For example:
1# Adding elements to a 2D array
2matrix.append([11, 12, 13])
3print(matrix) # Output: [[1, 2, 3], [4, 5, 10], [7, 8, 9], [11, 12, 13]]In this example, we add a new row with elements [11, 12, 13] to the matrix.
Removing Elements
To remove a row from a 2D array, you can use the pop() method and specify the index of the row to be removed. For example:
1# Removing elements from a 2D array
2matrix.pop(1)
3print(matrix) # Output: [[1, 2, 3], [7, 8, 9], [11, 12, 13]]In this example, we remove the second row from the matrix.
Multi-dimensional arrays are commonly used in image processing, robotics, and computer vision. They provide a way to represent and manipulate multi-dimensional data efficiently.
xxxxxxxxxx# Creating a 2D arraymatrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]# Accessing elements in a 2D arrayprint(matrix[0][1]) # Output: 2# Modifying elements in a 2D arraymatrix[1][2] = 10print(matrix) # Output: [[1, 2, 3], [4, 5, 10], [7, 8, 9]]# Finding the length of a 2D arrayprint(len(matrix)) # Output: 3print(len(matrix[0])) # Output: 3# Adding elements to a 2D arraymatrix.append([11, 12, 13])print(matrix) # Output: [[1, 2, 3], [4, 5, 10], [7, 8, 9], [11, 12, 13]]# Removing elements from a 2D arraymatrix.pop(1)print(matrix) # Output: [[1, 2, 3], [7, 8, 9], [11, 12, 13]]


