Home > Backend Development > Python Tutorial > python multidimensional slicing colon and three points

python multidimensional slicing colon and three points

不言
Release: 2018-04-19 11:14:42
Original
2773 people have browsed it

The following is an introduction to the use of colons and three dots in python multi-dimensional slicing. It has a good reference value and I hope it will be helpful to everyone. Let’s take a look together

Beginners learning python and numpy will summarize the problems encountered in the process of learning multi-dimensional slicing.

I won’t talk about one-dimensional slices, which are relatively simple. Let’s talk about two-dimensional ones first. It will be easy to understand the two-dimensional ones and the multi-dimensional ones. For example, first create a 5x5 two-dimensional array

Multi-dimensional slices are taken according to each dimension

Here, 2-3 in the first dimension and 3-5 in the second dimension are output respectively (the index starts from 0).

Here, take all the rows and take the 3-5th column.

This should be the most confusing part for everyone. Why is the column parameter changed to None and the output shape changed? Everyone needs to know here that None means adding a new one. Dimension, it has an alias called newaxis. You can know it by outputting numpy.newaxis, so this alias should be as the name suggests. So why is it 5x1x5 instead of 5x5x1? That’s because you used None in the second dimension. If you use it in the third dimension, it will become 5x5x1. If you don’t believe it, look at

Now everyone should understand. That is to say, in which dimension None is placed, a new dimension will appear in that dimension.

Let’s look at something even more bizarre

What the hell are the three dots? From memory, isn’t this a line-breaking operation? But it’s not here, it is Omit all colons and replace them with ellipses. You can see that the output of a[:, :, None] and a[..., None] is the same, because... replaces the first two colons. It should be clear now.

As for the three-dimensional and above, it is exactly the same as the two-dimensional.



##

The above is the detailed content of python multidimensional slicing colon and three points. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template