在调试时......它说我在线路hr [i] = h1上有索引超出范围错误。怎么解决?

时间:2015-04-02 07:59:06

标签: python indexoutofboundsexception

name = raw_input("Enter file:")
hr = list()
freq = list()
i=0
k=0
handle = open(name)
for line in handle:
    if line.startswith('From'):
        h1 = line.split()[5].split(':')[0]
        if h1 not in hr:
            hr[i] = h1
            i=i+1

for j in range(hr):
     for line in handle:
        if line.startswith('From'):
            h2 = line.split()[5].split(':')[0]
            if hr[j] == h2:
                freq[j] = k+1

for l in range(hr): 
      print hr[l],freq[l]

1 个答案:

答案 0 :(得分:1)

您引用列表中的第一项" h [0]"列表中没有项目。 Python不会隐式附加项目。请改用:

hr.append(h1)