Home >Backend Development >Python Tutorial >How to get the specified row and column of numpy array

How to get the specified row and column of numpy array

php中世界最好的语言
php中世界最好的语言Original
2018-04-09 10:55:309735browse

This time I will show you how to get the numpyarrayspecify the rows and columns, and what are the notesfor getting the rows and rows of the numpy array. Here is a practical case, let’s take a look.

This operation on the numpy array feels a bit troublesome, but there is nothing we can do.

For example

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

Take 2 3 rows and 1 2 columns of a

c=[1,2] 
d =[0,1]

If written as

b = a[c,d] 
output: 
[4 8]

the data in the first column of the second row and the second column of the third row are taken

This is not the result we want.

The correct approach is:

b = a[c]先取想要的行数据 
b = b[:,d] 
print(b) 
output: 
[[4 5] 
[7 8]]

This is the result we want. You must go through these two steps to complete.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Perfect solution to python2.7 being unable to use pip

##How to read and write txt files line by line in python

The above is the detailed content of How to get the specified row and column of numpy array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn