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

学习 时间:2026-04-03 20:02:54 阅读:7022
关于用python写一个函数,基础题请大家帮我看看这道题要求函数least_vowel_words(text)可以找出一段text中,哪个单词的元音占的比例最小例如:“take”就是 2/4=0.5>>>least_vowel_words("i do go to school.")["do", "go"]谢谢各位!

最佳回答

愤怒的水杯

震动的高山

2026-04-03 20:02:54

不应该是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())]再问: ������һ�£���ȷ���ԣ�ֻ�����ǻ�ûѧ��������õķ������� �����м

最新回答共有2条回答

  • 贤惠的八宝粥
    回复
    2026-04-03 20:02:54

    不应该是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())]再问: ������һ�£���ȷ���ԣ�ֻ�����ǻ�ûѧ��������õķ������� �����м

上一篇 入迷 知晓 振奋写出近义词

下一篇 Rt三角形BMC中,斜边BM=5,它在平面ABC上的射影AB长为4,角MBC=60°,求MC与平面ABC所成角的正弦值.