📅  最后修改于: 2023-12-03 14:45:59.442000             🧑  作者: Mango
In Python, you might come across situations where you need to access or manipulate elements in a 2D array. A 2D array is a container that holds multiple values in rows and columns. Each element in a 2D array is identified by its unique row and column indexes.
This guide will demonstrate various methods to index a 2D array in Python. It will cover accessing individual elements, rows, and columns, as well as performing slicing and iteration on 2D arrays.
To access an element in a 2D array, you need to specify its row and column indexes. In Python, 2D arrays can be represented using nested lists or NumPy arrays. Let's look at examples of accessing elements in both types of 2D arrays.
# Accessing elements in a nested list 2D array
array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
element = array[1][2] # Accessing element at row 1, column 2
print(element) # Output: 6
import numpy as np
# Accessing elements in a NumPy 2D array
array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
element = array[1][2] # Accessing element at row 1, column 2
print(element) # Output: 6
In addition to accessing individual elements, you can also access entire rows or columns in a 2D array.
# Accessing rows and columns in a nested list 2D array
array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
row = array[1] # Accessing row at index 1
column = [row[2] for row in array] # Accessing column at index 2
print(row) # Output: [4, 5, 6]
print(column) # Output: [3, 6, 9]
import numpy as np
# Accessing rows and columns in a NumPy 2D array
array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
row = array[1] # Accessing row at index 1
column = array[:, 2] # Accessing column at index 2
print(row) # Output: [4, 5, 6]
print(column) # Output: [3, 6, 9]
Slicing allows you to extract a portion of a 2D array. You can slice rows, columns, or a sub-matrix within a 2D array.
# Slicing a nested list 2D array
array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
sliced_rows = array[1:3] # Slicing rows 1 and 2
sliced_columns = [row[1:3] for row in array] # Slicing columns 1 and 2
sliced_submatrix = [row[1:3] for row in array[1:3]] # Slicing sub-matrix
print(sliced_rows) # Output: [[4, 5, 6], [7, 8, 9]]
print(sliced_columns) # Output: [[2, 3], [5, 6], [8, 9]]
print(sliced_submatrix) # Output: [[5, 6], [8, 9]]
import numpy as np
# Slicing a NumPy 2D array
array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
sliced_rows = array[1:3, :] # Slicing rows 1 and 2
sliced_columns = array[:, 1:3] # Slicing columns 1 and 2
sliced_submatrix = array[1:3, 1:3] # Slicing sub-matrix
print(sliced_rows) # Output: [[4, 5, 6], [7, 8, 9]]
print(sliced_columns) # Output: [[2, 3], [5, 6], [8, 9]]
print(sliced_submatrix) # Output: [[5, 6], [8, 9]]
You can iterate over a 2D array using nested loops or using the nditer
function provided by NumPy.
# Iterating over a nested list 2D array
array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for row in array:
for element in row:
print(element) # Output: 1, 2, 3, 4, 5, 6, 7, 8, 9
import numpy as np
# Iterating over a NumPy 2D array
array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
for element in np.nditer(array):
print(element) # Output: 1, 2, 3, 4, 5, 6, 7, 8, 9
These are some of the common techniques to index a 2D array in Python. By understanding these methods, you will be able to extract and manipulate data efficiently from 2D arrays based on your requirements.