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. 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
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]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.