# Task 5: How to reverse the columns of a 2D array with NumPy? Write a program to reverse the columns of a 2D array. Sample data : [[0, 1, 2],[3, 4, 5],[6, 7, 8]] Sample output : [[2 1 0] [5 4 3] [8 7 6]]
时间: 2023-07-02 15:11:39 浏览: 42
Sure, here's a solution using NumPy:
```python
import numpy as np
# Define the 2D array
arr = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
# Reverse the columns
rev_arr = arr[:, ::-1]
print("Original array:\n", arr)
print("Reversed array:\n", rev_arr)
```
This will output:
```
Original array:
[[0 1 2]
[3 4 5]
[6 7 8]]
Reversed array:
[[2 1 0]
[5 4 3]
[8 7 6]]
```
Here, we use the slicing syntax `[:, ::-1]` to reverse the columns of the array. The first colon `:` means we want to select all rows, and the `::-1` means we want to select the columns in reverse order.
I hope this helps! Let me know if you have any other questions.
相关问题
How to reverse the columns of a 2D array with NumPy? Write a program to reverse the columns of a 2D array Sample data: [[0,1,2],[3,4,5],[6,7,8]] Sample output:[[210][5 4 3][8 76]]
Here's a code snippet that uses NumPy to reverse the columns of a 2D array:
```python
import numpy as np
# Define the 2D array
arr = np.array([[0,1,2],[3,4,5],[6,7,8]])
# Reverse the columns
arr_reversed = arr[:, ::-1]
# Print the original and reversed arrays
print("Original array:")
print(arr)
print("Reversed array:")
print(arr_reversed)
```
The `::` syntax is used to slice the array. The first `:` indicates that we want to select all rows, and the `::-1` indicates that we want to reverse the order of the columns.
And here is the output for the given sample data:
```
Original array:
[[0 1 2]
[3 4 5]
[6 7 8]]
Reversed array:
[[2 1 0]
[5 4 3]
[8 7 6]]
```
As you can see, the columns have been reversed in the output array.
numpy slicing
NumPy slicing refers to the process of selecting a subset of elements from an array based on their indices, using the `[]` indexing operator. Slicing in NumPy works in a similar way to Python lists, but with some additional features.
The basic syntax for slicing a NumPy array is as follows:
```
array[start:stop:step]
```
where `start` is the index of the first element to include, `stop` is the index of the first element to exclude, and `step` is the spacing between the selected elements.
Here are some examples of NumPy slicing:
```python
import numpy as np
# Create a 1D array from 0 to 9
arr = np.arange(10)
# Select elements 2 through 5 (exclusive)
print(arr[2:5]) # Output: [2 3 4]
# Select every other element starting from the first
print(arr[::2]) # Output: [0 2 4 6 8]
# Reverse the order of the array
print(arr[::-1]) # Output: [9 8 7 6 5 4 3 2 1 0]
# Create a 2D array
arr2d = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
# Select the first two rows and the first two columns
print(arr2d[:2, :2]) # Output: [[0 1] [3 4]]
# Select the last row and the last two columns
print(arr2d[-1:, -2:]) # Output: [[7 8]]
```
In addition to these basic slicing operations, NumPy also supports advanced indexing, which allows you to select elements based on Boolean masks or integer arrays.