Python 类型错误:只有一个元素的整数数组可以转换为索引 3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33144039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:51:26  来源:igfitidea点击:

TypeError: only integer arrays with one element can be converted to an index 3

pythonarrayslistnumpyappend

提问by GeoffreyB

I'm having this error in the title, and don't know what's wrong. It's working when I use np.hstack instead of np.append, but I would like to make this faster, so use append.

我在标题中有这个错误,不知道出了什么问题。当我使用 np.hstack 而不是 np.append 时它正在工作,但我想让它更快,所以使用附加。

time_list a list of floats

heights is a 1d np.array of floats

time_list 浮点数列表

高度是浮点数的一维 np.array

j = 0
n = 30
time_interval = 1200
axe_x = []

while j < np.size(time_list,0)-time_interval:
    if (not np.isnan(heights[j])) and (not np.isnan(heights[j+time_interval])):
        axe_x.append(time_list[np.arange(j+n,j+(time_interval-n))])


 File "....", line .., in <module>
    axe_x.append(time_list[np.arange(j+n,j+(time_interval-n))])

TypeError: only integer arrays with one element can be converted to an index

采纳答案by Anand S Kumar

The issue is just as the error indicates, time_listis a normal python list, and hence you cannot index it using another list (unless the other list is an array with single element). Example -

问题正如错误所指示的那样,time_list是一个普通的 python 列表,因此您不能使用另一个列表对其进行索引(除非另一个列表是一个具有单个元素的数组)。例子 -

In [136]: time_list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]

In [137]: time_list[np.arange(5,6)]
Out[137]: 6

In [138]: time_list[np.arange(5,7)]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-138-ea5b650a8877> in <module>()
----> 1 time_list[np.arange(5,7)]

TypeError: only integer arrays with one element can be converted to an index

If you want to do that kind of indexing, you would need to make time_lista numpy.array. Example -

如果你想做那种索引,你需要做time_list一个numpy.array. 例子 -

In [141]: time_list = np.array(time_list)

In [142]: time_list[np.arange(5,6)]
Out[142]: array([6])

In [143]: time_list[np.arange(5,7)]
Out[143]: array([6, 7])


Another thing to note would be that in your whileloop, you never increase j, so it may end-up with infinite loop , you should also increase jby some amount (maybe time_interval?).

另一件需要注意的事情是,在你的while循环中,你永远不会增加j,所以它可能以无限循环结束,你也应该增加j一些(也许time_interval?)。



But according to the requirement you posted in comments -

但是根据您在评论中发布的要求-

axe_x should be a 1d array of floats generated from the time_list list

axe_x 应该是从 time_list 列表生成的一维浮点数组

You should use .extend()instead of .append(), .appendwould create a list of arrays for you. But if you need a 1D array, you need to use .extend(). Example -

您应该使用.extend()而不是.append(),.append将为您创建一个数组列表。但是如果你需要一个一维数组,你需要使用.extend(). 例子 -

time_list = np.array(time_list)
while j < np.size(time_list,0)-time_interval:
    if (not np.isnan(heights[j])) and (not np.isnan(heights[j+time_interval])):
        axe_x.extend(time_list[np.arange(j+n,j+(time_interval-n))])
        j += time_interval           #Or what you want to increase it by.