Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “如何修复”;列表分配索引超出范围“;?_Python_List - Fatal编程技术网

Python “如何修复”;列表分配索引超出范围“;?

Python “如何修复”;列表分配索引超出范围“;?,python,list,Python,List,我正在用Python实现用于预测路径损耗的IEEE 802.16d模型 我创建了两个独立的文件:一个是包含IEEE模型函数的库,另一个是调用函数的主代码 lib文件中的代码: def PL_IEEE_80216d(fc,d,Type,htx,hrx,corr_fact,mod=False): ''' IEEE 802.16d model Inputs: fc: Carrier frequency [Hz] d: D

我正在用Python实现用于预测路径损耗的IEEE 802.16d模型

我创建了两个独立的文件:一个是包含IEEE模型函数的库,另一个是调用函数的主代码

lib文件中的代码:

def PL_IEEE_80216d(fc,d,Type,htx,hrx,corr_fact,mod=False):
'''
    IEEE 802.16d model
    Inputs:
         fc:        Carrier frequency [Hz]
         d:         Distance between transmitter and receiver station [m]
         htx:       Height of transmitter [m]
         hrx:       Height of receiver [m]
         corr_fact: Correlation coefficient
         mod:       Modified [True/False]
'''
PL=[]
lambda = 3.10e8/fc
d0=100
if(Type == 'A'):
    a=4.6;b=0.0075;c=12.6
elif(Type == 'B'):
    a=4;b=0.0065;c=17.1
elif(Type == 'C'):
    a=3.6;b=0.005;c=20
else:
    a=0;b=0;c=0
if(corr_fact == 'AT&T'):
    if( Type == 'A' or Type == 'B'):
        C_rx = -10.8 * log10(hrx/2)
    else:
        C_rx = -20 * log10(hrx/2)
elif(corr_fact == 'OKUMURA'):
    if(hrx <= 3):
        C_rx = -10 * log10(hrx/3)
    else:
        C_rx = -20 * log10(hrx/3)
else:
    C_rx = 0
gamma=a-b*htx+c/htx
Cf=6*log10(fc/2000)
if(mod == True):
    d0_pr = d0 * 10 **(-(Cf+C_rx)/(10*gamma))
else:
    d0_pr = d0
A = 20 * log10(4*pi*d0_pr/lambda)+Cf+C_rx
for i in range(0,len(d)):
    print('i=%d'%i)
    if(d[i] > d0_pr):
        PL[i]=A+10*gamma*log10(d[i]/d0)
    else:
        PL[i]=20*log10(4*pi*d[i]/lambda)
    PL = PL.append(P[i])
return PL
但是,当我运行代码时,我得到一个错误:

列表分配索引超出范围


我不知道它在哪里超出了范围,尽管我已经尝试了很多次来解决它。

因为您已经将PL初始化为空列表,所以不能使用PL[I]。
改用append函数
PL.append(…)

我不知道它在哪里超出范围
——错误的堆栈跟踪没有告诉您这一点吗?通常情况下,不能将索引分配给列表中不存在的索引。所以如果你有
PL=[]
,那么
PL[1]=10
是一个错误,因为
PL[1]
不存在。哦,我明白了,我想如果我将PL初始化为空并用计算的PL[I]附加它,PL就会变成PL={PL[0],…PL[I]}。所以我必须用已知的值初始化PL,对吗???
from model_lib import *
import matplotlib.pyplot as plt
fc=2e9
htx=[30,30]
hrx=[2,10]
distance=arrange(1,3)  #2 distance=[1,2] => d[0]=1,d[1]=2
print("length of distance is %d"%len(distance))
for k in range(0,2):
     print('k=%d'%k)
     y_IEEE16d[k]=PL_IEEE_80216d(fc,distance,'A',htx[k],hrx[k],'AT&T')