Python: 创建一个计算文本文件中特定单词的函数
Python: Creating a function counting specific words in a textfile
我想创建一个函数,returns 文本文件中特定单词的字数计数值。
这是我目前拥有的:
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")
哪个returns:"the ; 35"
这正是我想要它做的。但是,如果我想测试文本中的一系列单词怎么办?我想要列表或字典中的单词(键)和值。有什么方法可以不使用模块来做到这一点?
假设我使用以下单词列表测试函数:[time, when, left, I, do, an, who, what, sometimes]。
我想要的结果是这样的:
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
我想做一个类似的风格,但只针对特定的词,有什么建议吗?
根据您提供的代码,我没有对此进行测试。
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
更新
尝试以下操作:
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])
我想创建一个函数,returns 文本文件中特定单词的字数计数值。
这是我目前拥有的:
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")
哪个returns:"the ; 35"
这正是我想要它做的。但是,如果我想测试文本中的一系列单词怎么办?我想要列表或字典中的单词(键)和值。有什么方法可以不使用模块来做到这一点?
假设我使用以下单词列表测试函数:[time, when, left, I, do, an, who, what, sometimes]。
我想要的结果是这样的:
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
我想做一个类似的风格,但只针对特定的词,有什么建议吗?
根据您提供的代码,我没有对此进行测试。
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
更新
尝试以下操作:
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])