简体   繁体   English

使用python从文本文件返回唯一单词出现的总和

[英]Return sum of unique word occurrences from a text file using python

I am trying to update an older script of mine for a class. 我正在尝试为课程更新我的旧脚本。 Typically this script will just record a '1' if any of the items from a list appear in that line. 通常,如果列表中的任何项目出现在该行中,则此脚本只会记录“ 1”。 However, now I want it to count and sum the number of times any of those unique words appear in that line. 但是,现在我希望它计算和总结那些唯一单词在该行中出现的次数。 For example, using this list: 例如,使用以下列表:

ess = ['jim','bob','sally','tom']
.
.
.   
elif 'SCHOOL' in line:
    csvfile.write( str(line.count(',') + 1)+ ',')
    flag = 0
    for staff in ess:
      if staff in line:
        csvfile.write('1')
        flag = 1
        break
    if flag == 1:
      csvfile.write('\n')
    else:
      csvfile.write('0\n')

Instead of simply recording a "1" if any of the names appear. 如果出现任何名称,而不是简单地记录“ 1”。 I would like it return a sum of how many names appear in that line. 我希望它返回该行中出现多少个名称的总和。 For example, if both jim and sally appear in that line, return a "2" 例如,如果吉姆和莎莉都出现在该行中,则返回“ 2”

You can simply run multiple count on the string/sentence. 您可以简单地对字符串/句子运行多个计数。

or you can split the sentence by " " (space) delimiter and go over that returned list and check each word is equal to one of your desired words: 或者您也可以split为“”(空格)分隔这句话,走了过来说返回的列表,并检查每个字等于你想要的话之一:

I would of do something like that: 我会做这样的事情:

 sum = 0
 for line in lines:
     split_list = line.split(" ")
     for word in ess:
         if word in split_list :
             sum+=1

or alternative: sum = 0 for line in lines: for word in ess: sum += line.count(word) 或替代:sum = 0表示行中的行:for ess中的单词:sum + = line.count(word)

There are two ways to attempt this: 有两种方法可以尝试此操作:

1) You do not care how many times a name appears in the sentence as long as the name appears at least once: 1)您不在乎一个名称出现在句子中的次数,只要该名称至少出现一次即可:

def names_in_sentence(sentence_str):
    return sum([1 for name in lis_names if name in sentence_str])

This is a fairly pythonic way of doing it. 这是一种相当蟒蛇的方法。 I am using list comprehension to create a list of 1's for each name that is there in the input sentence. 我正在使用列表推导为输入句子中存在的每个名称创建一个1的列表。

2) You do care about how many times a name appears in the sentence. 2)您确实关心一个句子中一个名字出现了多少次。 ie if the sentence was "This is a bob, he's quite the bob', you would return 2: 例如,如果句子是“这是一个鲍勃,他是个鲍勃”,您将返回2:

def names_in_sentence(sentence_str):
    return sum([sentence.count(name) for name in lis_names])

In this case, I use list comprehension to count the number of times each name appears in the sentence, and sum the list. 在这种情况下,我使用列表推导来计算每个名称在句子中出现的次数,并对列表求和。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM