在函数调用不同的文件中,有多少个单词以某个字符结尾



所以我正在尝试创建一个小函数,该函数将文件名作为参数,并返回文件中有多少个单词以"!","?"或"."结尾。

到目前为止,我已经尝试了以下方法:

def count_possible_sentences(file_name):
    with open(file_name) as wordfile:
        text_str = wordfile.read()
        word_list = text_str.split()
    count = 0
    for ch in word_list:
        if ch in "!?.":
            count += 1
    return count

但这不起作用,并且不计算在单独的调用文件中有多少个单独的 WORDS 以这些指定的字符结尾。我想拆分每个单词并循环遍历每个字符,如果它包含其中一个字符,它会在计数中添加 +1,但我不确定如何做到这一点。

编辑:还想只使用 .count? 那行得通吗? 干杯

编辑2:这是我试图让它们通过的文档测试:

def count_possible_sentences(file_name):
    """
    >>> count_possible_sentences("frances_oldham_kelsey.txt")
    45
    >>> count_possible_sentences("ernest_rutherford.txt")
    32
    >>> count_possible_sentences("marie_curie.txt")
    24
    """

这是指向失败.txt的链接:https://pastebin.com/raw/1NYPeY29

它说预期:45 得到:52

使用:

def count_possible_sentences(file_name):
    count = 0
    with open(file_name) as wordfile:              #Open file for read
        for line in wordfile:                      #Iterate each line
            for word in line.strip().split():      #Get words
                if word.endswith(("!", "?", ".")):  #Check if word ends with
                    count += 1
    return count

行...

if ch in '?!.':

。不会按预期工作。您需要单独检查每个标点符号:

if any(ch.endswith(punc) for punc in '?!.'):
# or
if any(punc in ch for punc in '?!.'):

如果你更喜欢列表理解,你可以做这一行。w[-1] in '!?.'检查单词的最后一个字符。

def count_possible_sentences(file_name):
    return len([w for w in open(file_name).read().split() if w[-1] in '!?.'])
print(count_possible_sentences('input_file.txt'))

我会建议使用正则表达式进行类似的事情。我认为这就是你想做的。

import re
pattern = "[a-zA-Z]+(!|?|.)"
string = "This! sent contains? 3 of those! words !!"
a = len(re.findall(pattern, string))
print(a) # 3

您需要更改 if 语句:

def count_possible_sentences(file_name):
    with open(file_name) as word_file:
        text_str = word_file.read()
        word_list = text_str.split()
    count = 0
    for item in word_list:
        if '!' in item or '?' in item or '.' in item:
             count += 1
    return count

相关内容

最新更新