Python报错TypeError:float与list无法执行+运算求助
Let's break down exactly what's causing this error and fix your code step by step:
1. You're generating single floats instead of arrays for ar and ter
Right now, your code uses random.uniform() which returns a single float, but you're trying to index into it like an array in the inner loop:
# Wrong: returns one float, not an array of na values ar = random.uniform(1,na) ter = 2 * cmath.pi * random.uniform(1,na)
Replace these lines with numpy's uniform generator to create an array of na random values:
# Correct: generates an array of na random floats between 1 and na ar = np.random.uniform(1, na, size=na) ter = 2 * np.pi * np.random.uniform(1, na, size=na)
We switch to np.pi here too, since it plays nicer with numpy arrays than cmath.pi.
2. Invalid list indexing on your numpy array w
The line w[n/2 + 1 + [j]] uses [j] (a single-element list) as an index, which doesn't work with numpy arrays. Plus, n/2 gives a float, which we need to convert to an integer for valid indexing:
# Convert n/2 to integer (n is even, so this is safe) w_index = int(n/2) + 1 + j # Add a quick check to avoid index out of bounds if w_index >= len(w): w_index = len(w) - 1 w_value = w[w_index]
3. Using math.cos on numpy arrays
math.cos() only handles single floats, but (w_value - wc) * td is a numpy array (since td is an array). Use np.cos() instead to handle array operations properly:
np.cos((w_value - wc) * td + ter[j])
4. Clean up the pha update
Use += for cleaner array addition, and make sure all operations stay within numpy's array system to avoid type mismatches.
Full Corrected Code
Putting all these fixes together, here's your updated code:
import math import numpy as np Tp = .1e-6 Xc = 2.e3 c = 3e8 B0 = 100e6 X0 = 50 w0 = 2 * np.pi * B0 fc = 1e9 wc = 2 * np.pi * fc alpha = w0 / Tp wcm = wc - alpha * Tp Ts = (2 * (Xc - X0)) / c Tf = (2 * (Xc - X0)) / c + Tp dt = np.pi / (2 * alpha * Tp) n = 2 * math.ceil((0.5 * (Tf - Ts)) / dt) t = Ts + np.arange(0, n*dt, dt) dw = 2 * np.pi / (n * dt) w = wc + dw * np.arange(-n/2, n/2) xn = [0, 35, 36.5, -25] fn = [1, 0.8, 1, 0.8] ntarget = 4 # SIMULATION s = np.zeros((1,n)) na = 8 # Generate array of random values instead of single floats ar = np.random.uniform(1, na, size=na) ter = 2 * np.pi * np.random.uniform(1, na, size=na) for i in np.arange(ntarget): td = t - (2 * (Xc + xn[i]) / c) pha = wcm * td + alpha * np.power(td, 2) for j in np.arange(na): w_index = int(n/2) + 1 + j # Prevent index out of bounds if w_index >= len(w): w_index = len(w) - 1 w_value = w[w_index] # Use np.cos for array-compatible cosine calculation pha += ar[j] * np.cos((w_value - wc) * td + ter[j]) print('pha :', pha)
Quick Notes
- I removed
cmathandrandomimports since we're using numpy's equivalents which work better with arrays. - The index check ensures you don't accidentally access a position outside the
warray's length. - This code should now output the array of values you're expecting, matching your desired format like
[-940.7325, -729.3720,......,6.6187e4,6.6449e4].
内容的提问来源于stack exchange,提问作者Lokesh

