Explain in detail the symbol in python array... and the difference between: symbol
- 2021-10-11 18:57:54
- OfStack
I don't know if you have ever seen the use of... symbol in python array, because I encountered this symbol when reading other people's code some time ago, so I hereby record 1. Let's look at one piece of code first:
import numpy as np
x = np.array([[1, 3],
[5, 6],
[8, 10]])
print(" Use '...' The result of the symbol is: ")
print(x[..., 0])
print(" Use ':' The result of the symbol is: ")
print(x[:, 0])
"""
Use '...' The result of the symbol is:
[1 5 8]
Use ':' The result of the symbol is:
[1 5 8]
"""
Comparing the results, it is not difficult to find that it can be concluded that in python array, the function of... symbol is equivalent to: symbol. But is this really the case? The answer is no. Let's look at the case of 3-dimensional arrays.
import numpy as np
x = np.array([[[0, 1],
[2, 3]],
[[4, 5],
[5, 6]],
[[7, 8],
[9, 10]]])
print(" Use '...' The result of the symbol is: ")
print(x[..., 0])
print(" Use two ':' The result of the symbol is: ")
print(x[:, :, 1])
print(" Use 1 A ':' The result of the symbol is: ")
print(x[:, 1])
"""
Use '...' The result of the symbol is:
[[0 2]
[4 5]
[7 9]]
Use two ':' The result of the symbol is:
[[ 1 3]
[ 5 6]
[ 8 10]]
Use 1 A ':' The result of the symbol is:
[[ 2 3]
[ 5 6]
[ 9 10]]
"""
We can see that the result of using the symbol... is the same as the result of using two: symbols, but it is different from the result of using a single: symbol. So we can get that symbols... are not exactly equivalent to symbols:.
Conclusion: For 2-dimensional arrays, the symbol... is equivalent to the symbol:, but for 3-dimensional arrays, it is not equal, considering the specific situation.