所以我想计算文本文件中每行的某些单词的出现次数。每个特定单词出现的次数无关紧要,每行发生的次数是多少次。我有一个包含单词列表的文件,由换行符分隔。它看起来像这样:
amazingly
astoundingly
awful
bloody
exceptionally
frightfully
.....
very
然后我有另一个包含文本行的文本文件。让我们举个例子说:
frightfully frightfully amazingly Male. Don't forget male
green flag stops? bloody bloody bloody bloody
I'm biased.
LOOKS like he was headed very
green flag stops?
amazingly exceptionally exceptionally
astoundingly
hello world
我希望我的输出看起来像:
3
4
0
1
0
3
1
这是我的代码:
def checkLine(line):
count = 0
with open("intensifiers.txt") as f:
for word in f:
if word[:-1] in line:
count += 1
print count
for line in open("intense.txt", "r"):
checkLine(line)
这是我的实际输出:
4
1
0
1
0
2
1
0
任何想法?
答案 0 :(得分:1)
这个怎么样:
def checkLine(line):
with open("intensifiers.txt") as fh:
line_words = line.rstrip().split(' ')
check_words = [word.rstrip() for word in fh]
print sum(line_words.count(w) for w in check_words)
for line in open("intense.txt", "r"):
checkLine(line)
输出:
3
4
0
1
0
3
1
0