切片索引必须是整数,它已经是整数了。如何超越这一点?

2024-04-25 07:03:19 发布

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

我在一个数组和一个矩阵上测试了这个函数;它给出了关于切片索引的相同类型错误。所以我的指标实际上是一个整数。你知道吗

def getpar(PSI):
    n = PSI.shape[0]
    x = n//2+1
    gamma = PSI[0:x]
    c = PSI[(x + 1):n]
    return gamma,c

getpar(np.matrix([[1,2,3],[1,2,3],[2,4,5],[2,5,2]]))

看来x的计算是个问题。你知道吗

> Traceback (most recent call last):
  File "C:\Users\Azerty\PycharmProjects\toto\venv37\lib\site-packages\IPython\core\interactiveshell.py", line 3325, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-20-f29d42dbbc56>", line 1, in <module>
    getpar(np.matrix([[1,2,3],[1,2,3],[2,4,5],[2,5,2]]))
  File "C:/Users/Azerty/PycharmProjects/toto/venv37/HARST/HARST.py", line 12, in getpar
    x = itn//2+1
  File "C:\Users\Azerty\PycharmProjects\toto\venv37\lib\site-packages\numpy\matrixlib\defmatrix.py", line 195, in __getitem__
    out = N.ndarray.__getitem__(self, index)
TypeError: slice indices must be integers or None or have an __index__ method

Tags: inpyselfnplineusersmatrixfile