forked from china-testing/python-api-tesing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslicing.py
More file actions
executable file
·123 lines (97 loc) · 2.02 KB
/
slicing.py
File metadata and controls
executable file
·123 lines (97 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import numpy as np
# Chapter 2 Beginning with NumPy fundamentals
#
# Demonstrates multi dimensional arrays slicing.
#
# Run from the commandline with
#
# python slicing.py
print("In: b = arange(24).reshape(2,3,4)")
b = np.arange(24).reshape(2,3,4)
print("In: b.shape")
print(b.shape)
#Out: (2, 3, 4)
print("In: b")
print(b)
#Out:
#array([[[ 0, 1, 2, 3],
# [ 4, 5, 6, 7],
# [ 8, 9, 10, 11]],
#
# [[12, 13, 14, 15],
# [16, 17, 18, 19],
# [20, 21, 22, 23]]])
print("In: b[0,0,0]")
print(b[0,0,0])
#Out: 0
print("In: b[:,0,0]")
print(b[:,0,0])
#Out: array([ 0, 12])
print("In: b[0]")
print(b[0])
#Out:
#array([[ 0, 1, 2, 3],
# [ 4, 5, 6, 7],
# [ 8, 9, 10, 11]])
print("In: b[0, :, :]")
print(b[0, :, :])
#Out:
#array([[ 0, 1, 2, 3],
# [ 4, 5, 6, 7],
# [ 8, 9, 10, 11]])
print("In: b[0, ...]")
print(b[0, ...])
#Out:
#array([[ 0, 1, 2, 3],
# [ 4, 5, 6, 7],
# [ 8, 9, 10, 11]])
print("In: b[0,1]")
print(b[0,1])
#Out: array([4, 5, 6, 7])
print("In: b[0,1,::2]")
print(b[0,1,::2])
#Out: array([4, 6])
print("In: b[...,1]")
print(b[...,1])
#Out:
#array([[ 1, 5, 9],
# [13, 17, 21]])
print("In: b[:,1]")
print(b[:,1])
#Out:
#array([[ 4, 5, 6, 7],
# [16, 17, 18, 19]])
print("In: b[0,:,1]")
print(b[0,:,1])
#Out: array([1, 5, 9])
print("In: b[0,:,-1]")
print(b[0,:,-1])
#Out: array([ 3, 7, 11])
print("In: b[0,::-1, -1]")
print(b[0,::-1, -1])
#Out: array([11, 7, 3])
print("In: b[0,::2,-1]")
print(b[0,::2,-1])
#Out: array([ 3, 11])
print("In: b[::-1]")
print(b[::-1])
#Out:
#array([[[12, 13, 14, 15],
# [16, 17, 18, 19],
# [20, 21, 22, 23]],
#
# [[ 0, 1, 2, 3],
# [ 4, 5, 6, 7],
# [ 8, 9, 10, 11]]])
print("In: s = slice(None, None, -1)")
s = slice(None, None, -1)
print("In: b[(s, s, s)]")
print(b[(s, s, s)])
#Out:
#array([[[23, 22, 21, 20],
# [19, 18, 17, 16],
# [15, 14, 13, 12]],
#
# [[11, 10, 9, 8],
# [ 7, 6, 5, 4],
# [ 3, 2, 1, 0]]])