我想创建一个函数来返回文本文件中特定单词的字数计数的值。
这是我目前所拥有的:
def Word_Counter(Text_File, Word):
Data = open(Text_File, 'r').read().lower()
count = Data.count(Word)
print(Word, "; ", count)
Word_Counter('Example.txt', "the")
返回:"the;35“
这就是我想让它做的事情。但是,如果我想测试文本中的一系列单词,该怎么办呢?我想要列表或字典中的单词(键)和值。在不使用模块的情况下,有什么方法可以做到这一点?
假设我用这个单词列表测试了这个函数: time,when,left,I,do,an,who,what,time。
我希望得到的结果如下:
Word Counts = {'time': 1, 'when': 4, 'left': 0, 'I': 5, 'do': 2, 'an': 0, 'who': 1, 'what': 3, 'sometimes': 1}
我已经能够创建一个字典,对每个单词进行单词计数,如下例所示。
wordfreq = {}
for word in words.replace(',', ' ').split():
wordfreq[word] = wordfreq.setdefault(word, 0) + 1
我想做一个类似的风格,但只针对特定的单词,有什么建议吗?
发布于 2019-10-21 09:47:22
从您给定的代码中,我没有对此进行测试。
def Word_Counter(Text_File, word_list):
Data = open(Text_File, 'r').read().lower()
output = {}
for word in word_list:
output[word] = Data.count(Word)
或者你可以这样做
text = open("sample.txt", "r")
# Create an empty dictionary
d = dict()
# Loop through each line of the file
for line in text:
# Remove the leading spaces and newline character
line = line.strip()
# Convert the characters in line to
# lowercase to avoid case mismatch
line = line.lower()
# Split the line into words
words = line.split(" ")
# Iterate over each word in line
for word in words:
# Check if the word is already in dictionary
if word in d:
# Increment count of word by 1
d[word] = d[word] + 1
else:
# Add the word to dictionary with count 1
d[word] = 1
发布于 2019-10-21 09:53:11
更新
尝试以下操作:
keywords = ['the', 'that']
worddict = {}
with open('out.txt', 'r') as f:
text = f.read().split(' ') # or f.read().split(',')
for word in text:
worddict[word] = worddict[word]+1 if word in worddict else 1
print([{x, worddict[x]} for x in keywords])
https://stackoverflow.com/questions/58483592
复制相似问题