python - 数据类型定义 - TypeError
问题描述
import cmath
import math
import random
import time
P = []
V = []
Vin = []
def Compute_wn_win(n,V,Vin):
for i in range (0,n):
V.append(complex(math.cos(2*math.pi*i/n),math.sin(2*math.pi*i/n)))
Vin.append(1/(complex(math.cos(2*math.pi*i/n),math.sin(2*math.pi*i/n))))
Compute_wn_win(8,V,Vin)
for i in range(0,8):
random_number = random.uniform(-1.0,1.0)
P.append(random_number)
def FFT(P,V,n):
if(n==1):
return P[0]
else:
Peven = []
Podd = []
for i in range(0,n/2):
Peven.append(P[2*i])
Podd.append(P[(2*i)+1])
Vsquared = []
for i in range(0,n/2):
Vsquared.append(V[i]*V[i])
Sole = FFT(Peven,Vsquared,n/2)
Solo = FFT(Podd,Vsquared,n/2)
Sol = [0 for x in range(0,n)]
for i in range(0,n/2):
Sol[i] = Sole[i]+V[i]*Solo[i]
Sol[i+n/2] = Sole[i]-V[i]*Solo[i]
return Sol
Sol = FFT(P,V,8)
我是 Python 新手。我有以下代码。但是,我收到以下行错误Sole = FFT(Peven,Vsquared,n/2)
和Sol[i] = Sole[i]+V[i]*Solo[i]
. 但是,我已将 Sole、Solo 和 Sol 定义为列表数据类型,所以我不明白为什么它提到 float 数据类型没有属性getitem
确切的错误是
Traceback (most recent call last):
File "/Users/globetrekker/Documents/CS5050/Assignment7/Test_py.py", line 40, in <module>
Sol = FFT(P,V,8)
File "/Users/globetrekker/Documents/CS5050/Assignment7/Test_py.py", line 33, in FFT
Sole = FFT(Peven,Vsquared,n//2)
File "/Users/globetrekker/Documents/CS5050/Assignment7/Test_py.py", line 33, in FFT
Sole = FFT(Peven,Vsquared,n//2)
File "/Users/globetrekker/Documents/CS5050/Assignment7/Test_py.py", line 37, in FFT
Sol[i] = Sole[i]+V[i]*Solo[i]
TypeError: 'float' object has no attribute '__getitem__'
解决方案
推荐阅读
- android - 在现有应用程序上集成 Flutter
- python - Python列表 - 获取非唯一值的特定索引
- cmake - CMake - Eigen3_DIR-NOTFOUND
- r - R:计算时间序列中列值的百分比
- awk - AWK 使用 if-then 和 AND/OR 运算符打印匹配字符串
- bixby - 如何从结果视图中引用外部概念的价值?
- java - 对于具有多个单词的输入字符串 - 检查它们中的任何一个是否以其他字符串开头的最有效方法是什么?
- java - 如何使用 Selenium 在 type="time" 输入框中输入时间 - 元素不可交互错误
- django - 在 uwsgi Emperor 上托管 2 个不同的 django 网站时出现“无法访问站点”
- java - 是否有技术原因阻止 Java 中的 final 与 C++ 中的 const 一样严格?