
[英]Error “list indices must be integers or slices, not tuple” occurs when I try to use function parametrs as list indices
[英]When i try to reshape, it gives me an error saying "TypeError: list indices must be integers or slices, not tuple"
实际上,我正在尝试将 Matlab 代码转换为 python,当我尝试重塑时,它向我抛出了一个 TypeError,说“TypeError:列表索引必须是整数或切片,而不是元组”。
MATLAB
[file,path] = uigetfile('*.dwr');
fid = fopen(strcat(path,'/',file));
m5 = (fread(fid, '*uint8'));
m5=double(m5);
fclose(fid);
m6=m5(12514:end);
no_bin_ele=m5(12039:2:12218)+256*m5(12040:2:12218);
s1=size(m6);
s2=((no_bin_ele(1)*7+4)*360)*1;
n1=m6(1:s2);
j1=reshape(n1(1:end,1),no_bin_ele(1)*7+4,360*1);
Python
import numpy as np
with open('aa.dwr', 'rb') as fp:
m5 = np.fromstring(fp.read(), dtype='uint8')
m5 = m5.astype(float)
m5 = m5.tolist()
m6 = m5[12514:]
no_bin_ele = m5[12039:12218:2]+256*m5[12040:12218:2]
s1 = len(m6)
s2=((no_bin_ele[1]*7+4)*360)*1
s2 =int(s2)
n1=m6[1:s2]
j1 = np.reshape(n1[1: ,1], no_bin_ele[1]*7+4, 360*1)
错误
回溯(最近一次调用):文件“ppp.py”,第 26 行,在 j1 = np.reshape(n1[1: ,1], no_bin_ele[1]*7+4, 360*1) 类型错误:列表索引必须是整数或切片,而不是元组
请尝试将第二个和第三个参数括在括号中,将其压缩为 1:
j1 = np.reshape(n1[1: ,1], (no_bin_ele[1]*7+4, 360*1))
如图所示: https : //docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html我希望它有所帮助。
Python 等效代码。 我无权访问“aa.dwr”,因此请检查来自 matlab 的 j1 是否等于来自以下代码的 j1。
import numpy as np
with open('aa.dwr', 'rb') as fp:
m5 = np.fromstring(fp.read(), dtype='uint8')
m5 = m5.astype(float)
m5 = m5.tolist()
m6 = m5[12513:]
no_bin_ele = m5[12038:12219:2]+256*m5[12039:12219:2]
s1 = len(m6)
s2=((no_bin_ele[0]*7+4)*360)*1
s2 =int(s2)
n1=m6[:s2+1]
j1 = np.reshape(n1[0: ,0], (no_bin_ele[0]*7+4, 360*1))
检查此 MATLAB 行:
no_bin_ele=m5(12039:2:12218)+256*m5(12040:2:12218);
在 Octave 中,我验证了
12039:2:12218
产生 90 个值,12039 到 12217。
12040:2:12218
还产生 90、12040 到 12218
所以这条线正在对连续的对进行求和, m5[i]+256*m5[i+1]
因为它们是作为uint8
加载的,所以我认为这是一个uint16
值。
但在numpy
:
In [467]: np.arange(12039,12218,2).shape
Out[467]: (90,)
In [468]: np.arange(12040,12218,2).shape
Out[468]: (89,)
终点处理是不同的。 第二个切片终点应为 12219。
当m5
是一个数组(它应该是)时,这解释了广播错误:
no_bin_ele = m5[12039:12218:2]+256*m5[12040:12218:2]
转换m5
tolist()
没有帮助。 对于列表, *
表示复制, +
表示加入。 对于数组,它们是乘法和加法。 完全不同。
In [475]: alist = list(range(0,10))
In [476]: alist
Out[476]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [477]: alist[1:6:2] + 4*alist[2:6:2]
Out[477]: [1, 3, 5, 2, 4, 2, 4, 2, 4, 2, 4]
其余代码对列表进行排序,因为索引和切片是相同的 - 直到n1[1: ,1]
表达式。 这仅对 numpy 数组有效。
实际上,还有其他索引问题。 Python 索引从 0 开始。
no_bin_ele(1) # 1st element of the matlab matrix
no_bin_ele[0] # 1st element of the array
n1(1:end,1) # matlab matrix is 2d
n1[1: ,1] # n1 isn't 2d
n1 # should just be
其实我觉得最后几行应该是
s2=int(no_bin_ele[0]*7+4)*360)
n1=m6[:s2]
j1 = np.reshape(n1, (-1, 360)) # -1 stands in for no_bin_ele[0]*7+4
虽然这种重塑可能有order
问题。 MATLAB 是列order='F'
,如order='F'
,尾随维度最外层。
我真的很想看到一些示例数据来验证这些步骤。 仅通过阅读代码我可以推断出的内容是有限的。 但我对处理 12218+ 字节长的数据并不感兴趣。
同样,一个 Octave 示例:
>> n1 = 1:10;
>> reshape(n1, 5,2)
ans =
1 6
2 7
3 8
4 9
5 10
和麻木:
In [481]: n1 = np.arange(1,11)
In [482]: np.reshape(n1, (5,2))
Out[482]:
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10]])
In [483]: np.reshape(n1, (5,2),order='F')
Out[483]:
array([[ 1, 6],
[ 2, 7],
[ 3, 8],
[ 4, 9],
[ 5, 10]])
In [484]: np.reshape(n1, (2,5))
Out[484]:
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10]])
===
m5
是文件,读取为uint8
,无符号字节。
m6
是一个很大的尾随部分,我们希望将其重塑为 (n,360) 矩阵(或其转置)。
no_bin_ele
是较早的部分,显然是 2 个字节的数字,我们使用其中的第一个来选择m6
进行整形。
如果我们有此文件格式的文本描述,则进行此翻译可能会更容易。 在没有样本或描述的情况下推断 matlab 行为可能会出错。
这会重现您的错误消息:
In [432]: [1,2,3][1:,1]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-432-1613afcfe2a2> in <module>
----> 1 [1,2,3][1:,1]
TypeError: list indices must be integers or slices, not tuple
这意味着在
j1 = np.reshape(n1[1: ,1], no_bin_ele[1]*7+4, 360*1)
n1
是一个列表,当您使用numpy
数组样式索引时。
尝试n1.shape
时的错误具有相同的问题 - 列表没有shape
。
n1
来自m6
,而m6
来自m5
,而m5
来自tolist()
方法!
在 MATLAB 中,一切都是 MATRIX(单元格和结构体除外)。 在 Python 中列表是最接近的,但是通过添加numpy
,您会得到更像 MATLAB 的数组 - 除了它们的维数可以是 0、1、2 等。调试时注意变量的type
,如果是数组, shape
和dtype
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.