【问题标题】:Function for counting words of i or more vowels in Python?在Python中计算i个或更多元音的单词的函数?
【发布时间】:2016-12-05 23:45:44
【问题描述】:

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

问题 13b 要求创建字典。关键是字符串中的单词(字符串有多个单词)。该值是该单个单词中有多少个元音。问题是这样问:如果单词至少有 i 个元音,则将其附加到字典中(带有元音数量的单词)这个函数有两个参数。第一个是没有标点符号的字符串。第二个参数表示单词必须附加到字典中的元音数量。教授希望我将此函数称为函数 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 作为参数并返回 s 包含的元音数。字符串 s 可以包含大写和小写字符。 例如,函数调用 vowelCount('Amendment') 应该返回整数 3,因为 字母“A”和“e”出现了 3 次。

问题 13b(10 分) 编写一个名为 manyVowels() 的函数,它接受一个文本体 t 和一个整数 i,作为 参数。文本 t 仅包含小写字母和空格。 manyVowels() 应该返回一个字典,其中的键是 t 中至少包含 i 的所有单词 元音。每个键对应的值就是其中元音的个数。为了获得充分的信任, manyVowels() 必须调用问题 11a 中的辅助函数 vowelCount() 来确定 每个单词中的元音数。例如,如果输入文本包含单词“hello”,那么 "hello" 应该是字典中的一个键,它的值应该是 2,因为里面有 2 个元音 “你好”。 输入: 1. t,由小写字母和空格组成的文本 2. i,元音的阈值数量 返回:键值对字典,其中键是 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}

【问题讨论】:

  • collections.Counter 可能非常有用,特别是因为它已经返回了一个类似字典的对象,可以轻松地将其转换为普通字典。否则,请将您的问题限制在一个特定的问题上——我们不会解决您的作业问题。但是,如果您有一个特定问题并且不知道如何继续,请让我们知道您的问题是什么,您期望什么以及发生了什么。然后edit你的问题。
  • 我的具体问题是如何将第一个函数的输出附加到字典中?

标签: python dictionary python-3.4


【解决方案1】:

添加条件以仅添加具有足够元音的单词

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] = number 行将vowelCount(word) 的结果添加到您的字典中。但前提是元音数至少为i

【讨论】:

    【解决方案2】:
    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!

    【讨论】:

      【解决方案3】:

      您的代码需要一些调整:

      第一个函数应该返回一个值而不是打印它:

      return (countVowels)
      

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多