简体   繁体   English

Python输出帮助,使频率列表从另一个文件中读取

[英]Python output help, making a frequency list reading in from another file

So I know how to extract the words I need from the file, but what I don't quite understand is how to integrate a loop counting the frequency of my extracted words. 因此,我知道如何从文件中提取所需的单词,但是我不太了解的是如何集成一个对提取的单词的频率进行计数的循环。 Here's what I have: 这是我所拥有的:

myfile = open('sample.pos')
file = open('sample.sorted', 'w')
line = myfile.readline()
list =[]

while line:
     line = myfile.readline()
     line.strip()
     if len(line) > 1:
         list.append(line)
list.sort()

x=0
while x < len(list):

     ll=list[x].split()
     file.write(ll[1] + '\n')
     x = x +1

myfile.close()
file.close()

I wanted to take my first list and using a loop count my word frequencies using something like this. 我想列出我的第一个列表,并使用循环来计算类似这样的词频。

list = []  
list2 = []    

for word in list:
     if word in list2:
         list2.index(word)[1] += 1
else:
    list2.append([word,0])

I'm just incredibly stuck in how to integrate this with the file.write and my current code. 我只是难以置信地停留在如何将其与file.write和我的当前代码集成在一起。 The end result is to have each word listed on a separate line with its frequency. 最终结果是将每个单词及其频率列在单独的行中。 What I get currently is just a list. 我目前得到的只是一个列表。

myfile = open('sample.pos')
file = open('sample.sorted', 'w')
list =[]

for line in myfile:
  line.strip()
  if len(line) > 1:
    list.append(line)

list2 = []    

for word in list:
  if word in [x for x in list2[0]]:
     list2.index(word)[1] += 1
else:
  list2.append([word,0])

but using dict would probably be better 但是使用dict可能会更好

wordCount = {}
for word in list:
  if word in wordCount.keys():
    wordCount[word] += 1
  else:
    wordcount[word] = 0

then you can access any word's count by wordCount[word] 那么您可以通过wordCount[word]访问任何单词的计数

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

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