teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

質問が更新されたので追記

2017/07/24 13:34

投稿

shimizukawa
shimizukawa

スコア1847

answer CHANGED
@@ -1,4 +1,4 @@
1
- `list.__getitem__(self, item)` メソッドに `(slice(None, None, NOne), 0)` オブジェクトが渡され、 `list.__getitem__` はintまたはslideオブジェクトを期待しているため `TypeError('list indices must be integers or slices, not tuple')` 例外が発生します。
1
+ `list.__getitem__(self, item)` メソッドに `(slice(None, None, None), 0)` オブジェクトが渡され、 `list.__getitem__` はintまたはslideオブジェクトを期待しているため `TypeError('list indices must be integers or slices, not tuple')` 例外が発生します。
2
2
 
3
3
  ```Python
4
4
  >>> class List(list):
@@ -13,4 +13,25 @@
13
13
  File "<stdin>", line 1, in <module>
14
14
  File "<stdin>", line 4, in __getitem__
15
15
  TypeError: list indices must be integers or slices, not tuple
16
- ```
16
+ ```
17
+
18
+ ----------------
19
+
20
+ (質問が更新されたので追記)
21
+
22
+ 通常のlistオブジェクトでは上記のようにエラーになりますが、これが `numpy.array` オブジェクトであれば以下の様に動作します。
23
+
24
+ ```Python
25
+ >>> import numpy as np
26
+ >>> b = np.array([[1,2,3],[4,5,6],[7,8,9]])
27
+ >>> b
28
+ array([[1, 2, 3],
29
+ [4, 5, 6],
30
+ [7, 8, 9]])
31
+ >>> a = b[:,0]
32
+ >>> a
33
+ array([1, 4, 7])
34
+ >>> b[0,:]
35
+ array([1, 2, 3])
36
+ ```
37
+ これは、 `numpy.array.__getitem__` が前述のような `(slice(None, None, None), 0)` を解釈して多次元の行列データの断面を返すように実装されているため、このような動作になります。