如何解决“列表索引必须是整数或切片,而不是列表”错误?

时间:2019-07-19 02:32:15

标签: python arrays numpy indexing

我正在尝试研究numpy,并且刚刚学习了花式索引和argsort。为了习惯这两个,我编写了简单的代码,并不断收到错误消息

To ensure stability and production quality, normal GKE clusters only enable features that
are beta or higher. Alpha features are not enabled on normal clusters because they are not
production-ready or upgradeable.

是什么原因造成的?

'TypeError: list indices must be integers or slices, not list'

2 个答案:

答案 0 :(得分:3)

我不确定您要做什么。如果将两个列表转换为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])

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

答案 1 :(得分:0)

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

使用数字索引访问列表,由于您使用的是列表,因此python给您输入错误。

print(array[some_number])print(array)