如何从Python向量的特定索引列表中选择组件?

2024-10-04 07:35:58 发布

您现在位置:Python中文网/ 问答频道 /正文

这里有x1vals:

>>> x1Vals
[-0.33042515829906227, -0.1085082739900165, 0.93708611747433213, -0.19289496973017362, -0.94365384912207761, 0.43385903975568652, -0.46061140566051262, 0.82767432358782367, -0.24257307936591843, -0.1182761514447952, -0.29794617763330011, -0.87410892638408, -0.34732294121174467, 0.40646145339571249, -0.64082861589870865, -0.45680189916940073, 0.4688889876175073, -0.89399689430691298, 0.53549621114138612]

这是我要选择的x1Vals索引列表

^{pr2}$

但是,当我试图用Matlab的方法得到x1Vals的值时,我得到了一个错误:

>>> x1Vals[np.where(np.dot(XValsOnly,newweights) > 0)]

Traceback (most recent call last):
  File "<pyshell#69>", line 1, in <module>
    x1Vals[np.where(np.dot(XValsOnly,newweights) > 0)]
TypeError: list indices must be integers, not tuple
>>> np.where(np.dot(XValsOnly,newweights) > 0)

有办法吗?在


Tags: 方法most列表错误npwheredotmatlab
1条回答
网友
1楼 · 发布于 2024-10-04 07:35:58

问题是您的x1Vals是一个list对象,它不支持花哨的索引。您只需从中构建一个数组:

x1Vals = np.array(x1Vals)

你的方法会奏效的。在

更快的方法是使用np.take

^{pr2}$

相关问题 更多 >