np.roll
を使ってください。
python
1 import numpy as np
2 a = np . arange ( 0 , 160 ) . reshape ( ( 10 , 4 , 4 ) )
3 result = np . roll ( a , shift = - 1 , axis = 2 )
4 result [ : , : , - 1 ] = 0
5 print ( result )
6 """
7 array([[[ 1, 2, 3, 0],
8 [ 5, 6, 7, 0],
9 [ 9, 10, 11, 0],
10 [ 13, 14, 15, 0]],
11
12 [[ 17, 18, 19, 0],
13 [ 21, 22, 23, 0],
14 [ 25, 26, 27, 0],
15 [ 29, 30, 31, 0]],
16
17 [[ 33, 34, 35, 0],
18 [ 37, 38, 39, 0],
19 [ 41, 42, 43, 0],
20 [ 45, 46, 47, 0]],
21
22 [[ 49, 50, 51, 0],
23 [ 53, 54, 55, 0],
24 [ 57, 58, 59, 0],
25 [ 61, 62, 63, 0]],
26
27 [[ 65, 66, 67, 0],
28 [ 69, 70, 71, 0],
29 [ 73, 74, 75, 0],
30 [ 77, 78, 79, 0]],
31
32 [[ 81, 82, 83, 0],
33 [ 85, 86, 87, 0],
34 [ 89, 90, 91, 0],
35 [ 93, 94, 95, 0]],
36
37 [[ 97, 98, 99, 0],
38 [101, 102, 103, 0],
39 [105, 106, 107, 0],
40 [109, 110, 111, 0]],
41
42 [[113, 114, 115, 0],
43 [117, 118, 119, 0],
44 [121, 122, 123, 0],
45 [125, 126, 127, 0]],
46
47 [[129, 130, 131, 0],
48 [133, 134, 135, 0],
49 [137, 138, 139, 0],
50 [141, 142, 143, 0]],
51
52 [[145, 146, 147, 0],
53 [149, 150, 151, 0],
54 [153, 154, 155, 0],
55 [157, 158, 159, 0]]])
56 """
numpy.roll — NumPy v1.20 Manual
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/05/26 02:56