将一个数组放入另一个数组时出现 Python ValueError
我正在尝试将一个数组插入另一个数组,但我认为数组存在尺寸问题,从而导致 ValueError。我试图在 EXP 中插入的指数段按我的预期进行打印,但在其上运行 len() 返回 1。为什么打印多个元素的数组会返回 len() 1?代码片段如下:
SPR = 48000 # Hz
duration = 0.2 # second
t = numpy.linspace(0, duration, duration * SPR)
p_list = [0, numpy.pi, 0]
SIGNALS = [(16000 * numpy.sin((2 * numpy.pi * t * 20) + p)).astype('int16')
for p in p_list]
EXP = [(16000 * (2**(-100*t))).astype('int16')]
e=EXP[0:4200]
print(e)
print(len(e))
SIGNALS[0][600:4800] = e
返回
[array([16000, 15976, 15953, ..., 0, 0, 0], dtype=int16)]
1
Traceback (most recent call last):
File "/home/pi/Experiments/actronika-exp.py", line 87, in <module>
SIGNALS[0][600:4800] = e
ValueError: setting an array element with a sequence.
I'm trying to insert one array into another, but I think I'm having a dimensioning issue with the arrays, leading to a ValueError. The exponential segment I'm trying to insert lives in EXP and prints as I'd expect, but running len() on it returns 1. Why would an array that prints with more than one element return a len() of 1? Code snippet below:
SPR = 48000 # Hz
duration = 0.2 # second
t = numpy.linspace(0, duration, duration * SPR)
p_list = [0, numpy.pi, 0]
SIGNALS = [(16000 * numpy.sin((2 * numpy.pi * t * 20) + p)).astype('int16')
for p in p_list]
EXP = [(16000 * (2**(-100*t))).astype('int16')]
e=EXP[0:4200]
print(e)
print(len(e))
SIGNALS[0][600:4800] = e
returns
[array([16000, 15976, 15953, ..., 0, 0, 0], dtype=int16)]
1
Traceback (most recent call last):
File "/home/pi/Experiments/actronika-exp.py", line 87, in <module>
SIGNALS[0][600:4800] = e
ValueError: setting an array element with a sequence.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是,当您执行以下操作时,您正在将数组插入列表中:
因此 X 是一个内部有数组的列表,我认为您应该这样做:
但是,如果您需要列表中的数组,则应该将此行更改
为
现在您正在获取列表 EXP 内的第一个数组。
The problem is that you are inserting the array inside a list when you do:
Thus X is a list with a array inside, I think you should just do:
However, if you need the array inside list thing, you should change this line
to
Now you are taking the first array, inside the list EXP.
这个 (
e
) 是 python 列表中的一个 numpy 数组。len(e)
返回列表的长度,为 1,因为它包含 1 个元素:numpy 数组This (
e
) is a numpy array inside a python list.len(e)
returns the list's length, which is 1, since it contains 1 element: the numpy array