回答編集履歴
6
d
answer
CHANGED
@@ -42,8 +42,8 @@
|
|
42
42
|
for で回すのでもいいと思いますが、一応、別解も書いておきます。
|
43
43
|
|
44
44
|
1. [numpy.lexsort](https://docs.scipy.org/doc/numpy/reference/generated/numpy.lexsort.html) で点一覧を xyz 順にソートする。
|
45
|
-
1. `
|
45
|
+
1. `x_pos = np.nonzero(np.diff(points[:, 0]))[0] + 1` で x 座標が変わるインデックスを調べる。
|
46
|
-
1. `
|
46
|
+
1. `y_pos = np.nonzero(np.diff(points[:, 1]))[0] + 1` で y 座標が変わるインデックスを調べる。
|
47
47
|
1. `np.unique(np.concatenate([x_pos, y_pos]))` で x または y 座標が変わるインデックスを取得する。
|
48
48
|
1. x または y 座標が変わるインデックスの位置で点一覧を分割する。
|
49
49
|
|
5
d
answer
CHANGED
@@ -71,5 +71,5 @@
|
|
71
71
|
|
72
72
|
for group in split_points:
|
73
73
|
print(f"x={group[0, 0]}, y={group[0, 1]}")
|
74
|
-
print(group)
|
74
|
+
print(group)
|
75
75
|
```
|
4
d
answer
CHANGED
@@ -62,9 +62,9 @@
|
|
62
62
|
print(points)
|
63
63
|
|
64
64
|
# xy の値が変わるインデックスを調べる。
|
65
|
-
|
65
|
+
x_pos = np.nonzero(np.diff(points[:, 0]))[0] + 1
|
66
|
-
|
66
|
+
y_pos = np.nonzero(np.diff(points[:, 1]))[0] + 1
|
67
|
-
xy_pos = np.unique(np.concatenate([x_pos, y_pos]))
|
67
|
+
xy_pos = np.unique(np.concatenate([x_pos, y_pos]))
|
68
68
|
|
69
69
|
# xy の値が変わるインデックスで分割する。
|
70
70
|
split_points = np.split(points, xy_pos)
|
3
d
answer
CHANGED
@@ -62,9 +62,9 @@
|
|
62
62
|
print(points)
|
63
63
|
|
64
64
|
# xy の値が変わるインデックスを調べる。
|
65
|
-
(x_pos,) = np.nonzero(np.diff(points[:, 0]))
|
65
|
+
(x_pos,) = np.nonzero(np.diff(points[:, 0]))
|
66
|
-
(y_pos,) = np.nonzero(np.diff(points[:, 1]))
|
66
|
+
(y_pos,) = np.nonzero(np.diff(points[:, 1]))
|
67
|
-
xy_pos = np.unique(np.concatenate([x_pos, y_pos]))
|
67
|
+
xy_pos = np.unique(np.concatenate([x_pos, y_pos])) + 1
|
68
68
|
|
69
69
|
# xy の値が変わるインデックスで分割する。
|
70
70
|
split_points = np.split(points, xy_pos)
|
2
d
answer
CHANGED
@@ -35,4 +35,41 @@
|
|
35
35
|
# [[1 2 2]
|
36
36
|
# [1 2 0]
|
37
37
|
# [1 2 0]]
|
38
|
+
```
|
39
|
+
|
40
|
+
## 追記
|
41
|
+
|
42
|
+
for で回すのでもいいと思いますが、一応、別解も書いておきます。
|
43
|
+
|
44
|
+
1. [numpy.lexsort](https://docs.scipy.org/doc/numpy/reference/generated/numpy.lexsort.html) で点一覧を xyz 順にソートする。
|
45
|
+
1. `(x_pos,) = np.nonzero(np.diff(points[:, 0]))` で x 座標が変わるインデックスを調べる。
|
46
|
+
1. `(y_pos,) = np.nonzero(np.diff(points[:, 1]))` で y 座標が変わるインデックスを調べる。
|
47
|
+
1. `np.unique(np.concatenate([x_pos, y_pos]))` で x または y 座標が変わるインデックスを取得する。
|
48
|
+
1. x または y 座標が変わるインデックスの位置で点一覧を分割する。
|
49
|
+
|
50
|
+
```python
|
51
|
+
import numpy as np
|
52
|
+
|
53
|
+
np.random.seed(0)
|
54
|
+
|
55
|
+
points = np.random.randint(0, 10, (20, 3))
|
56
|
+
print(points)
|
57
|
+
xy = (1, 2) # xy 座標
|
58
|
+
|
59
|
+
# xyz 順でソートする。
|
60
|
+
ind = np.lexsort((points[:, 2], points[:, 1], points[:, 0]))
|
61
|
+
points = points[ind]
|
62
|
+
print(points)
|
63
|
+
|
64
|
+
# xy の値が変わるインデックスを調べる。
|
65
|
+
(x_pos,) = np.nonzero(np.diff(points[:, 0])) + 1
|
66
|
+
(y_pos,) = np.nonzero(np.diff(points[:, 1])) + 1
|
67
|
+
xy_pos = np.unique(np.concatenate([x_pos, y_pos]))
|
68
|
+
|
69
|
+
# xy の値が変わるインデックスで分割する。
|
70
|
+
split_points = np.split(points, xy_pos)
|
71
|
+
|
72
|
+
for group in split_points:
|
73
|
+
print(f"x={group[0, 0]}, y={group[0, 1]}")
|
74
|
+
print(group)
|
38
75
|
```
|
1
d
answer
CHANGED
@@ -6,12 +6,33 @@
|
|
6
6
|
np.random.seed(0)
|
7
7
|
|
8
8
|
points = np.random.randint(0, 3, (20, 3))
|
9
|
+
print(points)
|
10
|
+
# [[0 1 0]
|
11
|
+
# [1 1 2]
|
12
|
+
# [0 2 0]
|
13
|
+
# [0 0 2]
|
14
|
+
# [1 2 2]
|
15
|
+
# [0 1 1]
|
16
|
+
# [1 1 0]
|
17
|
+
# [1 0 0]
|
18
|
+
# [1 2 0]
|
19
|
+
# [2 0 1]
|
20
|
+
# [1 2 0]
|
21
|
+
# [1 1 1]
|
22
|
+
# [0 2 0]
|
23
|
+
# [2 2 0]
|
24
|
+
# [2 0 0]
|
25
|
+
# [0 1 1]
|
26
|
+
# [2 0 0]
|
27
|
+
# [1 0 1]
|
28
|
+
# [2 2 0]
|
29
|
+
# [1 1 1]]
|
9
30
|
|
10
31
|
xy = (1, 2) # xy 座標
|
11
32
|
|
12
|
-
|
33
|
+
|
13
34
|
print(points[(points[:, 0] == xy[0]) & (points[:, 1] == xy[1])])
|
14
|
-
[[1 2 2]
|
35
|
+
# [[1 2 2]
|
15
|
-
|
36
|
+
# [1 2 0]
|
16
|
-
|
37
|
+
# [1 2 0]]
|
17
38
|
```
|