简体   繁体   English

Python中的2D数组索引

[英]2D-Array Indexing in python

I want to calculate how many times each word of an array is there in text file. 我想计算文本文件中数组的每个单词有多少次。 I am getting the output if write the print statement in shell. 如果在shell中编写print语句,则得到输出。 But not when I run it as a file . 但是当我将其作为文件运行时却没有。 I am getting this error "IndexError: list index out of range". 我收到此错误“ IndexError:列表索引超出范围”。 I am a beginner in python please help me out. 我是python的初学者,请帮助我。

from collections import Counter
from array import *
import string
cnt=Counter()
file = open('output.txt', 'r')
word =[ ]
c=[ ]
count =0
first_word =[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
word_count = [ ]
new_array =['CC','CD','DT','EX','FW','IN','JJ','JJR','JJS','LS','MD','NN','NNS','NNP','NNPS','PDT',
                       'POS','PRP','PRP$','RB','RBR','RBS','RP','SYM','TO','UH','VB','VBD','VBZ','WDT','WP$','WP','WRB']
for line in file:
      words = line.split()
      word.append(words)
for i in range(0,30):
      for j in range(0,33):
            if(new_array[j] in word[i][0]):
                  first_word[j]+=1
            else:
                  continue
print first_word

Don't use explicit values for range when you wan't to iterate over lists , rather use length of lists you are iterating. 当您不希望遍历lists ,不要对range使用显式值 ,而要使用要遍历的lists 长度 This way there will be no index errors . 这样就不会有索引错误 So, replace: 因此,请替换:

for i in range(0,30):
      for j in range(0,33):

with: 与:

for i in range(len(word)):
      for j in range(len(first_word)):

I guess it will fix the issue. 我想它将解决此问题。 Also when you have to initialize a list with similar values like: 同样,当您必须使用类似的值初始化列表时:

first_word =[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

there is a simple way in python for this as: 在python中有一个简单的方法是这样的:

>>> first_word = [0]*33
>>> first_word 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

I think the following code should get you the results you need: 我认为以下代码应为您提供所需的结果:

wordsFromFile = []    
f = open("output.txt", 'r')
for each_line in f:
    wordsFromFile.extend(each_line.strip().split(" "))
f.close()

print wordsFromFile

new_array = ['CC','CD','DT','EX','FW','IN','JJ','JJR','JJS','LS','MD','NN','NNS','NNP','NNPS','PDT',
                       'POS','PRP','PRP$','RB','RBR','RBS','RP','SYM','TO','UH','VB','VBD','VBZ','WDT','WP$','WP','WRB']
first_word = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

for eachWordFromFile in wordsFromFile:
    if eachWordFromFile in new_array: 
        first_word[new_array.index(eachWordFromFile)] += 1

#output results: 
for i in range(0,33):
    print str(new_array[i]) + ": " + str(first_word[i])

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

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