关于用python写一个函数,基础题

关于用python写一个函数,基础题
请大家帮我看看这道题
要求函数least_vowel_words(text)
可以找出一段text中,哪个单词的元音占的比例最小
例如:
“take”就是 2/4=0.5
>>>least_vowel_words("i do go to school.")
["do", "go"]
谢谢各位!
细雨无声么 1年前 已收到1个回答 举报

gaoping2005 幼苗

共回答了23个问题采纳率:95.7% 举报

不应该是school么? 比例是0.333333333def least_vowel_words(text):
line=text.strip('.').split()
wdict=dict()
vowel='aeiouAEIOU'
for word in line:
num = 0.0
for chr in word:
if chr in vowel:
num+=1
r=str(float(num/len(word)))
if r in wdict:
wdict[r]=wdict[r]+[word]
else:
wdict[r]=[word]
print wdict[min(wdict.keys())]

1年前 追问

3

细雨无声么 举报

ллһ⣬ text = this morning is nice. how do you think?' codeû취ȥtextľźʺѽ

举报 gaoping2005

ͺ. import re def least_vowel_words(text): line=re.findall(r'w+', text) wdict=dict() vowel='aeiouAEIOU' for word in line: num = 0.0 for chr in word: if chr in vowel: num+=1 r=str(float(num/len(word))) if r in wdict: wdict[r]=wdict[r]+[word] else: wdict[r]=[word] print wdict[min(wdict.keys())]

细雨无声么 举报

һ£ȷԣֻǻûѧõķ м

举报 gaoping2005

Ǿreplaceɣ滻Ϳˡ import re def least_vowel_words(text): line=text.replace(","," ") line=line.replace("?"," ") line=line.replace("!"," ") line=line.replace("."," ") line=line.replace(";"," ") line=line.split() wdict=dict() vowel='aeiouAEIOU' for word in line: num = 0.0 for chr in word: if chr in vowel: num+=1 r=str(float(num/len(word))) if r in wdict: wdict[r]=wdict[r]+[word] else: wdict[r]=[word] print wdict[min(wdict.keys())]
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 17 q. 0.043 s. - webmaster@yulucn.com