首页 > 解决方案 > 如何计算 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))

标签: python-3.x

解决方案


我认为问题出在这里:

#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 

推荐阅读