Python numpy, ask me a question

Asked 2 years ago, Updated 2 years ago, 48 views

#_*_coding:utf-8 _*_

import numpy as np

lst = [
    [1,2,3],
    [4,5,6],
    [7,8,9]
    ]

arr = np.array(lst)
a = arr[0:2, 0:2]
print(a)

I'm studying for my Python exam The result is [[12] [45]]

a = arr[0:2, 0:2] I don't know the meaning of this part. Why does that result come out

python numpy

2022-09-22 18:36

1 Answers

The front row and the back row are the columns.

Slicing 0:2 columns in row 0:2

lst = [
    [1,2,3],
    [4,5,6],
    [7,8,9]
    ]

arr = np.array(lst)

The first subscript is for a row.

arr[0] only slices the first row. [1 2 3]

arr[0:2] slices to the second row. [[1 2 3] [4 5 6]]

The last subscript is about heat.

arr[0, 0:2] Slice only two columns in the first row. [1 2]

arr[0:2, 0:2] slices two columns in two rows. [[1 2] [4 5]]


2022-09-22 18:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.