我如何防止TypeError:将python列表复制到numpy数组时,列表索引必须是整数,而不是元组? [英] How can I prevent the TypeError: list indices must be integers, not tuple when copying a python list to a numpy array?
问题描述
我正尝试使用来自另一个名为mean_data的数组的数据来创建3个numpy数组/列表,如下所示:
I am trying to create 3 numpy arrays/lists using data from another array called mean_data as follows:
---> 39 R = np.array(mean_data[:,0])
40 P = np.array(mean_data[:,1])
41 Z = np.array(mean_data[:,2])
当我尝试运行程序时,出现错误消息:
When I try run the program I get the error:
TypeError: list indices must be integers, not tuple
mean_data列表看起来像此示例...
The mean_data list looks like this sample...
[6.0, 315.0, 4.8123788544375692e-06],
[6.5, 0.0, 2.259217450023793e-06],
[6.5, 45.0, 9.2823565008402673e-06],
[6.5, 90.0, 8.309270169336028e-06],
[6.5, 135.0, 6.4709418114245381e-05],
[6.5, 180.0, 1.7227922423558414e-05],
[6.5, 225.0, 1.2308522579848724e-05],
[6.5, 270.0, 2.6905672894824344e-05],
[6.5, 315.0, 2.2727114437176048e-05]]
我不知道如何防止此错误,我尝试过将mean_data创建为np.array并使用np.append向其中添加值,但这也不能解决问题.
I don't know how to prevent this error, I have tried creating mean_data as a np.array and using np.append to add values to it but that doesn't solve the problem either.
这是回溯(以前使用过ipython)
Here's the traceback (was using ipython before)
Traceback (most recent call last):
File "polarplot.py", line 36, in <module>
R = np.array(mean_data[:,0])
TypeError: list indices must be integers, not tuple
我尝试创建数组的另一种方法是:
And the other way I tried to create an array was:
mean_data = np.array([])
for ur, ua in it.product(uradius, uangle):
samepoints = (data[:,0]==ur) & (data[:,1]==ua)
if samepoints.sum() > 1: # check if there is more than one match
np.append(mean_data[ur, ua, np.mean(data[samepoints,-1])])
elif samepoints.sum() == 1:
np.append(mean_data, [ur, ua, data[samepoints,-1]])
对此的追溯是:
IndexError Traceback (most recent call last)
<ipython-input-3-5268bc25e75e> in <module>()
31 samepoints = (data[:,0]==ur) & (data[:,1]==ua)
32 if samepoints.sum() > 1: # check if there is more than one match
---> 33 np.append(mean_data[ur, ua, np.mean(data[samepoints,-1])])
34 elif samepoints.sum() == 1:
35 np.append(mean_data, [ur, ua, data[samepoints,-1]])
IndexError: invalid index
推荐答案
变量mean_data
是一个嵌套列表,在Python中,不能通过多维切片来访问嵌套列表,即:mean_data[1,2]
,而不是一个会写mean_data[1][2]
.
The variable mean_data
is a nested list, in Python accessing a nested list cannot be done by multi-dimensional slicing, i.e.: mean_data[1,2]
, instead one would write mean_data[1][2]
.
这是因为mean_data[2]
是一个列表.进一步的索引是递归完成的-因为mean_data[2]
是一个列表,所以mean_data[2][0]
是该列表的第一个索引.
This is becausemean_data[2]
is a list. Further indexing is done recursively - since mean_data[2]
is a list, mean_data[2][0]
is the first index of that list.
此外,mean_data[:][0]
不起作用,因为mean_data[:]
返回mean_data
.
Additionally, mean_data[:][0]
does not work because mean_data[:]
returns mean_data
.
解决方案是替换数组或导入原始数据,如下所示:
The solution is to replace the array ,or import the original data, as follows:
mean_data = np.array(mean_data)
numpy数组(像MATLAB数组和嵌套列表一样)支持使用元组的多维切片.
numpy arrays (like MATLAB arrays and unlike nested lists) support multi-dimensional slicing with tuples.
这篇关于我如何防止TypeError:将python列表复制到numpy数组时,列表索引必须是整数,而不是元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!