Python 如何返回文本文件中x个元音的数量
该函数使用文件名和x(这意味着返回文件名中的前2个或4个元音)。我写的代码返回元音,但我不确定它返回的是什么。代码应该通过doctest。我仍在努力找出答案,但如果有人对我做错了什么有任何建议,我将不胜感激,因为我对python还是比较陌生的 文件名的内容是:(“我有一束红玫瑰”)Python 如何返回文本文件中x个元音的数量,python,string,doctest,Python,String,Doctest,该函数使用文件名和x(这意味着返回文件名中的前2个或4个元音)。我写的代码返回元音,但我不确定它返回的是什么。代码应该通过doctest。我仍在努力找出答案,但如果有人对我做错了什么有任何建议,我将不胜感激,因为我对python还是比较陌生的 文件名的内容是:(“我有一束红玫瑰”) def return_元音(文件名,x): """ >>>返回_元音(“roses.txt”,2) “Ia”返回文本中的前两个元音 >>>返回_元音(“roses.txt”,3) “Iae”返回文本中的前三个元音 "
def return_元音(文件名,x):
"""
>>>返回_元音(“roses.txt”,2)
“Ia”返回文本中的前两个元音
>>>返回_元音(“roses.txt”,3)
“Iae”返回文本中的前三个元音
"""
文件=打开(文件名)
text_file=files.read()
辅音=“bcdfghjklmnpnpqrstvwxyzbcdfhjklmnpqrstvwxyz”#不需要辅音
s_随_元音=“”
索引=0
当指数
首先,您不需要定义嵌套的,而
和对于
循环,您只需要迭代该行,并在每个单词处检查它是辅音还是元音
如果结果是元音,那么只需增加计数,在迭代结束时检查计数是否超过传递的值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
def count\u vovels(文件名,x):
"""
>>>返回_元音(“roses.txt”,2)
“Ia”返回文本中的前两个元音
>>>返回_元音(“roses.txt”,3)
“Iae”返回文本中的前三个元音
"""
文件=打开(文件名为“r”)
text_file=files.read()
#辅音=“bcdfghjklmnpnpqrstvwxyzbcdfhjklmnpqrstvwxyz”#不需要辅音
元音=“aeiou”
s_随_元音=“”
索引=0
#当指数=x:
打破
文件。关闭()
用_元音返回s_
打印计数值(“sample.txt”,2)
>>>Ia
打印计数值(“sample.txt”,3)
>>>依斯克拉
打印计数值(“sample.txt”,4)
>>>国际原子能机构
根据我上面的评论,我修改了您的代码。我还做了一些其他的更改,所有这些更改都以####
#####x??当您在60亿个月后回到代码时,您还记得X的意思吗?
####def return_元音(文件名,x):
def return_元音(文件名,NumberOfVouelsTofId):
####我对您在doctest预期输出中的评论有问题(doctest预期它们)
"""
>>>返回_元音(“roses.txt”,2)
“Ia”
>>>返回_元音(“roses.txt”,3)
“依斯克拉汽车电器公司”
"""
####稍好一点的名字
textFile=open(文件名)
fileBody=textFile.read()
####使你的支票包括在内,而不是排他性的。
####如果你的档案里有“我有一束红玫瑰”怎么办?2将被视为元音
####辅音=“bcdfghjklmnpnpqrstvwxyzbcdfhjklmnpqrstvwxyz”#不需要辅音
元音='AEIOU'
####使用更有意义的变量名
####s_随_元音=“”
FirstVouelsFound=“”
索引=0
而索引
顺便说一句,这是上面代码的输出:
E:\coding\Python\Python\Python.exeC:/Users/me/PycharmProjects/untitled/vouels roses.txt尝试:
返回元音(“roses.txt”,2)应为:
“Ia”确定尝试:
返回元音(“roses.txt”,3)应为:
“Iae”ok 1项目没有测试:
main1个项目通过了所有测试:在main中进行了2次测试。在2个项目中返回\u元音2次测试。2通过,0失败。考试通过了 进程已完成,退出代码为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.
有几件事
集合元音=集合('aeiuoAEIOU')
检查集合中的成员身份比检查列表中的成员身份要快
res=[]
。
当添加到字符串s+=
时,它被视为
由于运行时间可能较长,特别是
使用其他实现,例如PyPy。这无关紧要
以你为例。但是编写好的代码不会有什么坏处with
语句打开文件。巨蟒会
当我回到“with````的级别时,请立即为我关闭它。加入(res)for
循环遍历文件的所有行。
一次在线使用,可以与
非常大的文件while
检查元音的限制
而是显式递增的计数器和for
循环。
当到达文件末尾时,for循环终止。
如果尚未找到限制,则while
可能永远持续
当
x
为4时应返回什么?是Iaea
还是I
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.