如何 return 文本文件中的 x 个元音字母

How to return the x amount of vowels in a text file

该函数接受一个文件名和 x(这意味着 return 文件名中的前 2 或 4 个元音)。我写的代码 returns 元音,但我不确定它是什么 returning。代码应该通过 doctest。我仍在努力弄清楚,但如果有人对我做错了什么有任何建议,我将不胜感激,因为我对 python.

还是比较陌生

文件名的内容是:("I have a bunch of red roses")

def return_vowels(filename, x):
    """
    >>> return_vowels("roses.txt", 2)
    'Ia' #returns the first two vowels in the text
    >>> return_vowels("roses.txt", 3)
    'Iae'#returns the first three vowels in the text
    """
    files = open(filename)
    text_file = files.read()
    consonants = "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz"#do not want consonants
    s_with_vowels = ""
    index = 0
    while index < x:
        for letter in read_files:
            if letter not in consonants:
                s_with_vowels += letter
        return s_with_vowels

if __name__=="__main__":
    import doctest
    doctest.testmod(verbose=True)

首先你不需要定义嵌套的whilefor循环,你只需要遍历该行并在每个单词检查它是辅音还是元音?

如果结果是元音字母,那么您只需增加计数,并在迭代结束时检查计数是否超过传递值 x

要记住的另一件事是,您检查的是带辅音的单词,它漏掉了许多字符,例如数字、特殊字符、空格等。所以所有这些都被算作元音。所以最好对照元音串来克服这个缺点。

def count_vovels(filename, x):
    """
    >>> return_vowels("roses.txt", 2)
    'Ia' #returns the first two vowels in the text
    >>> return_vowels("roses.txt", 3)
    'Iae'#returns the first three vowels in the text
    """
    files = open(filename, "r")
    text_file = files.read()
    #consonants = "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz"#do not want consonants
    vowels = "aeiou"
    s_with_vowels = ""
    index = 0
    #while index < x:
    for letter in text_file:
        if letter.lower() in vowels:
            s_with_vowels += letter
            index+=1
        if index >=x:
            break
    files.close()
    return s_with_vowels

print count_vovels("sample.txt", 2)
>>> Ia
print count_vovels("sample.txt", 3)
>>> Iae
print count_vovels("sample.txt", 4)
>>> Iaea

我根据上面的评论修改了您的代码。我还做了一些其他更改,所有这些都以 ####

开头的注释标记
#### x??  Will you remember what X means when you come back to the code in 6b months time?
#### def return_vowels(filename, x):
def return_vowels(filename, numberOfVowelsToFind):

    #### I had problems with your comments in the doctest expected output (doctest expected them)
    """
    >>> return_vowels("roses.txt", 2)
    'Ia'

    >>> return_vowels("roses.txt", 3)
    'Iae'
    """

    #### slightly better name
    textFile = open(filename)
    fileBody = textFile.read()

    #### make your check inclusory, not exclusory.
    #### what if your file contained "I have a bunch of red roses"? 2 would be seen as a vowel
    #### consonants = "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz"#do not want consonants
    vowels = 'AEIOU'

    #### use a slightly more meaningful variable name
    #### s_with_vowels = ""
    firstVowelsFound = ""

    index = 0
    while index < numberOfVowelsToFind:
        for letter in fileBody:
            if letter.upper() in vowels:
                firstVowelsFound += letter

            #### You need this check to know when you are done
            if len(firstVowelsFound) == numberOfVowelsToFind:
                return firstVowelsFound

if __name__=="__main__":
    import doctest
    doctest.testmod(verbose=True)

Gratz 使用 doctest,顺便说一句。这是上面代码的输出:

E:\coding\Python\Python\python.exe
C:/Users/me/PycharmProjects/untitled/vowels roses.txt Trying:
return_vowels("roses.txt", 2) Expecting:
'Ia' ok Trying:
return_vowels("roses.txt", 3) Expecting:
'Iae' ok 1 items had no tests:
main 1 items passed all tests: 2 tests in main.return_vowels 2 tests in 2 items. 2 passed and 0 failed. Test passed.

Process finished with exit code 0

这按预期工作:

def find_vowels(file_name, limit):
    """
    >>> # returns the first two vowels in the text
    >>> find_vowels("roses.txt", 2)
    'Ia'
    >>> # returns the first two vowels in the text
    >>> find_vowels("roses.txt", 3)
    'Iae'
    """
    count = 0
    res = []
    vowels = set('aeiuoAEIOU')
    with open(file_name) as fobj:
        for line in fobj:
            for c in line:
                if c in vowels:
                    count += 1
                    res.append(c)
                if count >= limit:
                    break
    return ''.join(res)

if __name__=="__main__":
    import doctest
    doctest.testmod(verbose=True)

并通过测试:

Trying:
    find_vowels("roses.txt", 2)
Expecting:
    'Ia'
ok
Trying:
    find_vowels("roses.txt", 3)
Expecting:
    'Iae'
ok
1 items had no tests:
    __main__
1 items passed all tests:
   2 tests in __main__.find_vowels
2 tests in 2 items.
2 passed and 0 failed.
Test passed.    

几件事。

  1. 我用元音 set vowels = set('aeiuoAEIOU')
    在集合中检查成员资格比在列表中更快。

  2. 我用一个列表来保存找到的元音 res = []。 虽然添加到字符串 s += 有效,但它被认为是 反模式由于可能很长 运行 时间尤其如此 与 PyPy 等其他实现一起使用。这没关系 完全以您为例。但是编写好的代码不会有坏处。

  3. 我用with语句打开文件。 Python 会 一旦我缩减到 `with``` ''.join(res).

  4. 的级别,就立即为我关闭它
  5. 我使用 for 循环遍历文件的所有行。 一次在线使用可以使用 非常高效的大文件。

  6. 我不使用while来检查元音的限制 而是一个 counter 和一个 for 显式递增循环。 当到达文件末尾时,for 循环终止。 如果尚未找到限制,while 可能会永远持续下去。

  7. ''.join(res) 从我的结果列表中创建了一个字符串。