编写程序来计算不同的字母

时间:2017-10-22 17:15:07

标签: python string function count

我正在编写一个程序,它接受一个字符串和2个参数来计算属于指定参数的字母e的数量。我得到了第一个和最后一个可能性的正确计数,但没有得到两个步骤之间的正确计数。这是我的功能:

def count_letter_e(string_to_be_counted,ignore_case=True,ignore_accent=True):

    """
    Return the number of times 'e' and its variations appears in a string given the parameters specified.

    Parameters
    ----------
    string_to_be_counted: str
    A string containing e's that need to be counted

    Returns
    -------
    total: int
    Number of times 'e' and the specified variations appear in the given string
    """


    #counting individual letters to be used to calculate totals in if statements
    #Gets all counts of lowercase 'e'
    e_counted=string_to_be_counted.count('e')
    é_counted=string_to_be_counted.count('é')
    ê_counted=string_to_be_counted.count('ê')
    è_counted=string_to_be_counted.count('è')

    #Get all counts of Uppercase 'E'
    E_counted=string_to_be_counted.count('E')
    É_counted=string_to_be_counted.count('É')
    Ê_counted=string_to_be_counted.count('Ê')
    È_counted=string_to_be_counted.count('È')

    #Create a total variable
    total=0

    #check which parameters have been set
    if ignore_case == True and ignore_accent == True:
        total=e_counted + é_counted + ê_counted + è_counted + E_counted + É_counted + Ê_counted + È_counted
        return total
        total=0
    elif ignore_case == True and ignore_accent == False:
        total= e_counted + E_counted
        return total
        total=0
    elif ignore_case == False and ignore_accent == True:
        total= e_counted + é_counted + ê_counted + è_counted
        return total
        total=0
    elif ignore_case == False and ignore_accent == False:
        total=e_counted
        return total
        total=0

以下是我测试的句子:

sentence_1=("ThE weEk will bè frÊe until thÉre is a shÈèp that is freêd from thé pen")
sentence_2=("Thé redEyê fèlt likE a rÊal pain until I got hit in the hÊel by a freE sÈed")
sentence_3=("The frée pÊa made a gêtaway towards thé hèêl of a pÉnquin but only made it to the knEÈ")
sentence_4=("ThErÉ is a knêe that nèÊds to meÈt the queen for tÈsting of léaning pizza")

以下是每个输出与期望输出的比较 第1:14 v 14号句(这很好) setnence 2:7 v 8(这不好) 句3:10 v 7(这不好) 句4:5 v 5(这很好)

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:3)

以下是您功能的改进:

def count_letter_e(string_to_be_counted,ignore_case=True,ignore_accent=True):

    chars_to_count = { # chars that will be counted 
                       # based on the "ignore_case+ignore_accent" state

        (True, True):'eéêèEÉÊÈ', 
        (True, False):'eE', 
        (False, True):'eéêè', 
        (False, False):'e'
    }

    condition = (ignore_case, ignore_accent)
    result = 0

    for c in chars_to_count[condition]:
        result += string_to_be_counted.count(c)

    return result

或者以快捷方式相同:

def count_letter_e(string_to_be_counted,ignore_case=True,ignore_accent=True):

    chars_to_count = { 
        (True, True):'eéêèEÉÊÈ', 
        (True, False):'eE', 
        (False, True):'eéêè', 
        (False, False):'e'
    }

    return sum([string_to_be_counted.count(c) for c in chars_to_count[(ignore_case, ignore_accent)]])


这种方法的价值不仅在于显着的代码减少,而且在于函数的所有设置现在都在一个地方 - 在字典chars_to_count中 - 并且您可以快速灵活地更改它们其他计数任务。

结果:

sentence_1 = "ThE weEk will bè frÊe until thÉre is a shÈèp that is freêd from thé pen"
sentence_2 = "Thé redEyê fèlt likE a rÊal pain until I got hit in the hÊel by a freE sÈed"
sentence_3 = "The frée pÊa made a gêtaway towards thé hèêl of a pÉnquin but only made it to the knEÈ"
sentence_4 = "ThErÉ is a knêe that nèÊds to meÈt the queen for tÈsting of léaning pizza"

print(count_letter_e(sentence_1, True, True)) # 14
print(count_letter_e(sentence_2, True, False)) # 8
print(count_letter_e(sentence_3, False, True)) # 10
print(count_letter_e(sentence_4, False, False)) # 5


请注意,您的原始代码会产生相同的结果 并且似乎没有错误 - 基于程序的逻辑,期望的结果应该与上面的打印输出中的相同。

答案 1 :(得分:0)

我喜欢@MaximTitarenko的方法,但这是另一种选择。它不像DRY那样,但它使计数逻辑非常清晰。

def count_letter_e(string, ignore_case=True, ignore_accent=True):
    if ignore_case and ignore_accent:
        counts = [
            string.count('e'),
            string.count('é'),
            string.count('ê'),
            string.count('è'),
            string.count('E'),
            string.count('É'),
            string.count('Ê'),
            string.count('È'),
        ]
    elif ignore_case and not ignore_accent:
        counts = [
            string.count('e'),
            string.count('E'),
        ]
    elif not ignore_case and ignore_accent:
        counts = [
            string.count('e'),
            string.count('é'),
            string.count('ê'),
            string.count('è'),
        ]
    elif not ignore_case and not ignore_accent:
        counts = [
            string.count('e'),
        ]

    return sum(counts)

sentence_1 = 'ThE weEk will bè frÊe until thÉre is a shÈèp that is freêd from thé pen'
sentence_2 = 'Thé redEyê fèlt likE a rÊal pain until I got hit in the hÊel by a freE sÈed'
sentence_3 = 'The frée pÊa made a gêtaway towards thé hèêl of a pÉnquin but only made it to the knEÈ'
sentence_4 = 'ThErÉ is a knêe that nèÊds to meÈt the queen for tÈsting of léaning pizza'

print(count_letter_e(sentence_1, True, True))
print(count_letter_e(sentence_2, True, False))
print(count_letter_e(sentence_3, False, True))
print(count_letter_e(sentence_4, False, False))