简体   繁体   English

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

[英]How to fix "list assignment index is out of range"?

I am implementing IEEE 802.16d model for predicting path loss in Python我正在实施 IEEE 802.16d model 以预测 Python 中的路径损耗

I created 2 separated files: one is library containing function for IEEE model and the other is main code where function will be called我创建了 2 个单独的文件:一个是包含 IEEE model 的 function 的库,另一个是主要代码,其中将调用 function

Code in lib file: 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

My main code:我的主要代码:

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')

However when I ran the code, I get an error:但是,当我运行代码时,出现错误:

list assignment index is out of range列表分配索引超出范围

I don't know where it is out of range although I have tried many times to figure it out.我不知道它在哪里超出范围,尽管我已经尝试了很多次来弄清楚它。

Since you have initialized PL as an empty list, you cannot use PL[i].由于您已将 PL 初始化为一个空列表,因此您不能使用 PL[i]。 Use the append function instead.请改用 append function。 PL.append(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM