python-3.x - 如何计算 txt 文件中每行的单词实例
问题描述
我在计算 txt 文件中带有“联合”的行数时遇到问题。我可以计算 txt 文件中出现了多少次“联合”,但其中包含“联合”的行数不正确。
# Iterate through file_data and
# compute your counts in this cell
# ---------------------------------
file_data = []
with open('/dsa/data/all_datasets/hamilton-federalist-548.txt', 'r') as file:
# Hint: for line in file_data:
line_count = 0
word_count = 0
for line in file_data:
this_line_count = 0
# ------------ Add your code below --------------
#Loop through the array of words 'line'
for line in file:
line = line.strip()
split_line = line.split(' ')
file_data.append(split_line)
#For each word in the array, test it to 'union'
for line in file_data:
if line == line.count('union'):
line_count += 1 # Returns 'Lines: 0' - this is wrong.
for word in file_data:
word_count += word.count('union') # Returns 'Words: 35'
#if it's a match increment this_line_count
#at the end of the line loop add this_line_count to word_count
#if this_line_count isn't 0, line_count would increment by one
# ------------ =================== --------------
print('Lines: {}; Words: {}'.format(line_count, word_count))
解决方案
我认为问题出在这里:
#For each word in the array, test it to 'union'
for line in file_data:
if line == line.count('union'):
line_count += 1 # Returns 'Lines: 0' - this is wrong.
代替上面的代码,试试
for line in file_data:
if 'union' in line: # Check if 'union' is present in line, which is now a list of strings
line_count += 1
推荐阅读
- postgresql - 分析在 postgreSQL 中将文件上传到 API 时产生的错误
- gatling - 加特林检查soap响应是否包含节点
- java - 设备进入睡眠模式时服务无法正常工作
- python - 熊猫在 groupby 对象上应用 groupby
- kubernetes - GCE Ingress 未从就绪探测中获取健康检查
- python - 提供在烧瓶应用程序中下载 mpld3 图作为图像的选项
- ios - 从 SCNScene url 获取下载进度
- java - Swing - 从 JFileChooser 中选择的 JTree 多个根文件夹
- navision - 尝试将 PowerApps 与 Microsoft Navision 2016 连接时总是遇到以下错误
- geolocation - 地理定位中没有出现蓝点(Ionic 3)