计算 Python 中 i 个或多个元音的单词的函数?

Function for counting words of i or more vowels in Python?

在下面的代码中,问题 13a 要求我让函数计算一个字符串中有多少个元音字母。 (我不必在作业中调用该函数。)但是我调用它来测试它,那部分是完全正确的并且可以工作。字符串可以是大写和小写,没有标点符号。

问题 13b 要求创建字典。键是字符串中的单词(字符串有多个单词)。该值是该单个单词中有多少个元音。问题是这样问的:如果单词至少有 i 个元音,则将其附加到字典中(具有元音数量的单词)此函数有两个参数。第一个是没有标点符号的字符串。第二个参数表示单词必须附加到字典中的元音数量。教授要我调用 Function 13a 这个函数作为算法的一部分。也就是说,问题 13a 的输出是该问题中键(单个词)的值。我在回答这个问题时遇到了麻烦,因为我无法 Python 将 13a 的输出(一个单词的元音数量)附加到字典键。

而且在下面的代码中,我还没有处理应该使用变量 i 的部分。

这是我的代码:

    print("Question 13a")
    def vowelCount(s):
        vowels = 'aeiou'
        countVowels = 0
        for letter in s.lower():
            if letter in vowels:
                countVowels += 1
        print(countVowels)

    print("Question 13b")
    def manyVowels(t, i):
        my_string = t.split()
        my_dict = {}
        for word in my_string:
            number = vowelCount(word)
            my_dict[word].append(number)
        print(my_dict)    
    print(manyVowels('they are endowed by their creator with certain unalienable rights', 2))

如果您无法理解问题,请看教授的指示:

问题 13a(10 分) 字母 a、e、i、o 和 u 是元音字母。没有其他字母是元音。 编写一个名为 vowelCount() 的函数,它接受一个字符串 s 作为参数,returns s 包含的元音数。字符串 s 可以同时包含大写和小写字符。 例如,函数调用 vowelCount('Amendment') 应该 return 整数 3 因为 字母 'A' 和 'e'.

出现了 3 次

问题 13b(10 分) 编写一个名为 manyVowels() 的函数,它接受一段文本 t 和一个整数 i,如下所示 参数。文本 t 仅包含小写字母和白色 space。 manyVowels() 应该 return 一个字典,其中的键是 t 中至少包含 i 的所有单词 元音。每个键对应的值是其中元音的个数。为了获得满分, manyVowels() 必须调用问题 11a 中的辅助函数 vowelsCount() 以确定 每个单词中的元音数量。例如,如果输入文本包含单词 "hello",则 "hello" 应该是字典中的一个关键字,它的值应该是 2 因为里面有 2 个元音字母 "hello"。 输入: 1. t,由小写字母和白色组成的文本space 2. i,元音的阈值数量 Return:键值对的字典,其中的键是t中至少包含i的词 元音,每个键的值是它包含的元音数。 例如,以下将是正确的输出。

text = 'they are endowed by their creator with certain unalienable rights' print(manyVowels(text, 3)) {'certain': 3, 'unalienable': 6, 'creator': 3, 'endowed': 3}

您的代码需要一些调整:

第一个函数应该return一个值不打印它:

return (countVowels)

第二个功能是没有正确地将带有伴随值的键添加到字典中。你应该使用:

    my_dict[word] = number
return {k:v for k, v in my_dict.items() if v > i} 

添加条件以仅添加具有足够字数的单词

def vowelCount(s):
    vowels = 'aeiou'
    countVowels = 0
    for letter in s.lower():
        if letter in vowels:
            countVowels += 1
    return countVowels

def manyVowels(t, i):
    my_string = t.split()
    my_dict = {}
    for word in my_string:
        number = vowelCount(word)
        if number >= i:
            my_dict[word] = number
    return my_dict 

my_dict[word] = numbervowelCount(word) 的结果添加到您的字典中。但前提是 vovels 的数量至少为 i.

def vowelCount(s):
  num_vowels=0
  for char in s:
    if char in "aeiouAEIOU":
      num_vowels = num_vowels+1
  return num_vowels

def manyVowels(text, i):
  words_with_many_vowels = dict()
  text_array = text.split()
  for word in text_array:
    if vowelCount(word) >= i:
       words_with_many_vowels[word] = vowelCount(word)
  return words_with_many_vowels

print(vowelCount('Amendment'))

text = 'they are endowed by their creator with certain unalienable rights'
print(manyVowels(text, 3))

输出:

3
{'creator': 3, 'certain': 3, 'endowed': 3, 'unalienable': 6}

试一试here!