Home > OS >  Python ValueError when putting one array into another
Python ValueError when putting one array into another

Time:03-04

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.

CodePudding user response:

[array([16000, 15976, 15953, ...,     0,     0,     0], dtype=int16)]

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

CodePudding user response:

The problem is that you are inserting the array inside a list when you do:

X = [np.array([0, ...])]

Thus X is a list with a array inside, I think you should just do:

X = np.array([0, ...])

However, if you need the array inside list thing, you should change this line

e=EXP[0:4200]

to

e=EXP[0][0:4200]

Now you are taking the first array, inside the list EXP.

  • Related