Home  >  Article  >  Backend Development  >  Arrays and lists in Python: introduction to the use of colons

Arrays and lists in Python: introduction to the use of colons

不言
不言Original
2018-04-18 11:13:085591browse

The following is an introduction to the flexible usage of arrays and lists in Python: colon. It has a good reference value and I hope it will be helpful to everyone. Let’s take a look together

Let’s take a look at an example:

import numpy as np
x=np.array([[1,2,3],[5,6,7],[7,8,9]])
print(x)
Out[64]:
array([[1, 2, 3],
  [5, 6, 7],
  [7, 8, 9]])

I think everyone should have no problem with the above results. It just defines an np array. The key is below

##

print(x[:,::-1])
Out[65]:
[[3 2 1]
 [7 6 5]
 [9 8 7]]

The above code implements a function, which is to arrange the array in reverse order in each dimension. How to understand this code? This is a problem I encountered when doing deep learning style migration, that is, the image rgb to bgr, and then I saw the code written by others. At first, I thought about using transpose. For the explanation of transpose, you can refer to my blog. I won’t explain it here, but it actually doesn’t work because transpose is dimension exchange. Then I thought Why can you use double colons? After reading for a while, I figured it out. Let me explain below:

x[:,::-1], this code is actually the index, the first colon (comma (before) is obviously to select everything in the first dimension, that is, all the rows here, followed by two colons. Look at it this way, for example, our list y=[1,2,3],y[:2] The result is [1,2], that is, the first colon means starting from the first one. In fact, the first colon after our comma here also starts from the first one. What about the second colon? In fact, The second colon represents the end, y=[1,2,3],y[::], the result is [1,2,3], what is the third parameter? In fact, the third parameter is the step size. The length cannot be 0. If it is -1, it means reverse order. If it is 1, it means selecting all. If it is 2, it means taking every other one.

Look at the following code:

x=np.array([[1,2,3],[5,6,7],[7,8,9]])
print(x)
print('------------')
print(x[:,::-1])
print('------------')
print(x[:,::1])
print('------------')
print(x[:,::2])
print('------------')
print(x[:,::3])
print('------------')
print(x[:,::666666])
x=np.array([[1,2,3],[5,6,7],[7,8,9]])
print(x)
print('------------')
print(x[:,::-1])
print('------------')
print(x[:,::1])
print('------------')
print(x[:,::2])
print('------------')
print(x[:,::3])
print('------------')
print(x[:,::666666])
[[1 2 3]
 [5 6 7]
 [7 8 9]]
------------
[[3 2 1]
 [7 6 5]
 [9 8 7]]
------------
[[1 2 3]
 [5 6 7]
 [7 8 9]]
------------
[[1 3]
 [5 7]
 [7 9]]
------------
[[1]
 [5]
 [7]]
------------
[[1]
 [5]
 [7]]

You can understand from the above code, what follows The reason why x[:,::666666] is as large as 66666 is to say that starting from the first one, the following represents the step size. For a step size as large as 66666, we can only take the first one. In fact, the step size starts from 3. From the beginning, you can only get the first one.

The above usage is the same for the list.

Related recommendations:


For loop and range built-in functions in python


The above is the detailed content of Arrays and lists in Python: introduction to the use of colons. 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