我目前正在編寫一個程序,將採取一個文本文件,然後計算文件中的每個單詞的頻率,下面的每個單詞並剝離其標點符號後。循環通過數組 - 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