Countin words from one file, in another Python -
i trying count amount of times words file shows in file. directed link below, of assistance still isn't doing desired duty. can me?
https://codereview.stackexchange.com/questions/144074/program-to-count-vowels
def count_happyw(file): hap_count = 0 hwords in file.readlines(): line = file.readline() while line != "": item in hwords: if item in file: count_happyw[item] += 1 return hap_count
i tried
line = file.readline() total = 1 * [len(h_words) line in file.readline()] token in file.readlines(): while line != "": line = file.readline() item in h_words: if item in file: total = [1] * len(item)
yourwords.txt contains words searching space separated, contents:
apple orange bananna
yourfile.txt file search in:
apple orange bananna
an apple on orange tree
wordcount = {} open('yourwords.txt', 'r') f1, open('yourfile.txt', 'r') f2: words = f1.read().split() wordcount = { i:0 in words} line in f2: line_split = line.split() word in line_split: if word in wordcount: wordcount[word] += 1 print(wordcount)
output:
{'bananna': 1, 'apple': 2, 'orange': 2}
Comments
Post a Comment