Home > Article > Backend Development > python multidimensional slicing colon and three points
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!