关于用python写一个函数,基础题
来源:学生作业帮 编辑:神马作文网作业帮 分类:英语作业 时间:2024/11/17 10:36:19
关于用python写一个函数,基础题
请大家帮我看看这道题
要求函数least_vowel_words(text)
可以找出一段text中,哪个单词的元音占的比例最小
例如:
“take”就是 2/4=0.5
>>>least_vowel_words("i do go to school.")
["do", "go"]
谢谢各位!
请大家帮我看看这道题
要求函数least_vowel_words(text)
可以找出一段text中,哪个单词的元音占的比例最小
例如:
“take”就是 2/4=0.5
>>>least_vowel_words("i do go to school.")
["do", "go"]
谢谢各位!
不应该是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())]
再问: лл��������һ�����⣬ ���text = ��this morning is nice. how do you think?' ���codeû�취ȥ�����text����ľ�ź��ʺ�ѽ��
再答: ������ͺ���. 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())]
再问: ������һ�£���ȷ���ԣ�ֻ�����ǻ�ûѧ��������õķ������� �����м
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())]
再问: лл��������һ�����⣬ ���text = ��this morning is nice. how do you think?' ���codeû�취ȥ�����text����ľ�ź��ʺ�ѽ��
再答: ������ͺ���. 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())]
再问: ������һ�£���ȷ���ԣ�ֻ�����ǻ�ûѧ��������õķ������� �����м