2015-11-10 230 views
-2

我目前正在编写一个程序,将采取一个文本文件,然后计算文件中的每个单词的频率,下面的每个单词并剥离其标点符号后。循环通过数组 - 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 

但我看不出它如何能超出范围。据我所知,indexi都没有超出范围。我是Python的新手,并且在'for'循环语法中遇到了很多麻烦。任何提示将不胜感激。

+2

在python中,你可以简单地通过'list for item:'来遍历列表。你不需要使用'range(len(list)-1)'。如果您仍然需要访问索引,请使用枚举(列表)中的'for i,item':'。 –

+0

这是如何转换为循环数组的索引?或者我该如何在列表中“编号”我的物品?我无法绕过这个包裹。 –

+2

我真的建议不要在同一个源代码中同时使用'WORD'和'word'作为变量名称。 – TigerhawkT3

回答

1

在您的代码中,word[index]确实不存在。你应该做的是word.append(WORD[i])

+0

我得到一个不同的错误现在。如果(WORD [i])不在字中: TypeError:列表索引必须是整数,而不是str' 不知道如何解决这个问题。我是新来的Python类型。我以为我已经被认为是一个int了? –

+0

该行与您发布的内容不同。我认为这是一个单独的问题。 – Phonon

1

一个更好的办法是使用defaultdict:

>>> from collections import defaultdict 
>>> d = defaultdict(int) 
>>> for i in ["abc", "abc", "def"]: 
...  d[i] += 1 
... 
>>> d 
defaultdict(<type 'int'>, {'abc': 2, 'def': 1}) 
>>> 

这是计算的频率,而不是维护索引更Python的方式。这些单词在d.keys()中,它们的频率在d.values()

+1

甚至['''collections.Counter'''](https://docs.python.org/3/library/collections.html#collections.Counter) – wwii