Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何修复“列表索引必须是整数或片,而不是列表”错误?_Python_Arrays_Numpy_Indexing - Fatal编程技术网

Python 如何修复“列表索引必须是整数或片,而不是列表”错误?

Python 如何修复“列表索引必须是整数或片,而不是列表”错误?,python,arrays,numpy,indexing,Python,Arrays,Numpy,Indexing,我正在努力学习numpy,我刚刚学习了花式索引和argsort。为了适应这两种情况,我编写了简单的代码,并不断得到错误 'TypeError: list indices must be integers or slices, not list' 这是什么原因造成的 import numpy as np array = ['John','Mike','Sarah','Kate','Samuel'] score_array = [78, 95, 84, 98, 88] idx = np.argso

我正在努力学习numpy,我刚刚学习了花式索引和argsort。为了适应这两种情况,我编写了简单的代码,并不断得到错误

'TypeError: list indices must be integers or slices, not list'
这是什么原因造成的

import numpy as np
array = ['John','Mike','Sarah','Kate','Samuel']
score_array = [78, 95, 84, 98, 88]
idx = np.argsort(score_array)
idx = idx.tolist()
array[idx]

我不知道你想做什么。如果将这两个列表转换为numpy数组。它运行无误

import numpy as np

array = np.array(['John','Mike','Sarah','Kate','Samuel'])
score_array = np.array([78, 95, 84, 98, 88])
idx = np.argsort(score_array)
idx = idx.tolist()
print(array[idx])

因为数组现在是一个numpy数组,所以它接受数组样式的索引。

我不确定您想做什么。如果将这两个列表转换为numpy数组。它运行无误

import numpy as np

array = np.array(['John','Mike','Sarah','Kate','Samuel'])
score_array = np.array([78, 95, 84, 98, 88])
idx = np.argsort(score_array)
idx = idx.tolist()
print(array[idx])

因为数组现在是一个numpy数组,所以它接受数组样式的索引。

您正在将idx转换为一个列表,并尝试使用它访问另一个列表

列表是通过数字索引访问的,因为您使用的是列表,所以python会给您一个类型错误


printarray[some_number]或printarray

您正在将idx转换为一个列表,并尝试使用它访问另一个列表

列表是通过数字索引访问的,因为您使用的是列表,所以python会给您一个类型错误


printarray[some_number]或printarray

您试图使用numpy数组为常规列表编制索引。也许您想将列表转换为np.array?仅因为称为array并不意味着它接受numpy数组样式的索引。您正在尝试使用numpy数组对常规列表进行索引。也许您想将列表转换为np.array?仅仅因为称为array并不意味着它接受numpy数组样式的索引。哦,它应该是numpy数组!我在那一部分犯了错误。非常感谢你的帮助。酷。如果您愿意,您可以通过单击答案左侧的绿色勾号来接受答案。哦,它应该是numpy数组!我在那一部分犯了错误。非常感谢你的帮助。酷。如果您愿意,您可以通过单击答案左侧的绿色勾号来接受答案。