Supponendo che l'oggetto è davvero una matrice numpy
, questo è noto come advanced indexing, e raccoglie le colonne specificate:
>>> import numpy as np
>>> a = np.arange(12).reshape(3,4)
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> a[:, [1,2,3]]
array([[ 1, 2, 3],
[ 5, 6, 7],
[ 9, 10, 11]])
>>> a[:, [1,3]]
array([[ 1, 3],
[ 5, 7],
[ 9, 11]])
noti che questo non funzionerà con l'elenco standard di Python:
>>> a.tolist()
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]
>>> a.tolist()[:,[1,2,3]]
Traceback (most recent call last):
File "<ipython-input-17-7d77de02047a>", line 1, in <module>
a.tolist()[:,[1,2,3]]
TypeError: list indices must be integers, not tuple
Sei sicuro che 'list' è una lista? Potrebbe essere un'istanza di una classe che sovrascrive '__getitem__' in un modo insolito. – Kevin
Hai provato a digitare (elenco)? Potrebbe essere un array numpy o un dataframe panda. – mattexx
@mattexx, non ho l'intero codice, cosa fa questo in numpy e in panda? – Stephan