我目前正在编写一个程序,将采取一个文本文件,然后计算文件中的每个单词的频率,下面的每个单词并剥离其标点符号后。循环通过数组 - python
这里是我的代码:
import sys
import string
incoming =[]
freq =[]
word =[]
count = 0
index = 0
i = 0
with open(sys.argv[1], "r") as word_list:
for line in word_list:
#word is the string of the .txt file
#strips punctuation and lower cases each word
for words in line.split():
words = words.translate(string.maketrans("",""), string.punctuation)
words = words.lower()
incoming.append(words)
#incoming is now an array with each element as a word from the file
for i in range(len(incoming)-1):
if (incoming[i]) not in word:
#WORD[i] = word[index]
word[index] = incoming[i]
freq[index] = 1
index += 1
else:
freq[index] = freq[index] + 1
for j in word:
print "%s %d", word[j], freq[j]
我收到错误:
File "wordfreq.py", line 26, in <module>
word[index] = incoming[i]
IndexError: list assignment index out of range
但我看不出它如何能超出范围。据我所知,index
和i
都没有超出范围。我是Python的新手,并且在'for'循环语法中遇到了很多麻烦。任何提示将不胜感激。
在python中,你可以简单地通过'list for item:'来遍历列表。你不需要使用'range(len(list)-1)'。如果您仍然需要访问索引,请使用枚举(列表)中的'for i,item':'。 –
这是如何转换为循环数组的索引?或者我该如何在列表中“编号”我的物品?我无法绕过这个包裹。 –
我真的建议不要在同一个源代码中同时使用'WORD'和'word'作为变量名称。 – TigerhawkT3