pre_exam 4

这道题需要将给出的text里面的单词根据单词长短分类,并按字母顺序排列

def words(text):
    text=text.replace(',','').replace('\','').replace('?','').replace('.','').replace("",'').replace(':','').replace(';','')
    words_seperate=text.split()
    new_list=set()
    max_count=0
    for i in words_seperate:
        new_list.add(i.lower())
        if len(i)>max_count:
            max_count=len(i)
        #max_count=max(max_count,len(a)) dic_matching
={} for number in range(1,max_count+1): same_len=[] for i in new_list: if len(i)==number: same_len.append(i) order_same=sorted(same_len) dic_matching[number]=order_same for key,value in dic_matching.items(): if value: print('Words of length {}:'.format(key)) print(' ',value) a=words('You must not fall into the common error of mistaking these simpletons ’ ’for liars and hypocrites. They believe honestly and sincerely that their ’ ’diabolical inspiration is divine. Therefore you must be on your guard against ’ ’your natural compassion. You are all, I hope, merciful men: how else could you ’ ’have devoted your lives to the service of our gentle Savior? You are going ’ ’to see before you a young girl, pious and chaste; for I must tell you, gentlemen, ’ ’that the things said of her by our English friends are supported by no evidence, ’ ’whilst there is abundant testimony that her excesses have been excesses of religion ’ ’and charity and not of worldliness and wantonness.') print(a)


这里创捷一个新的set时用s=set(),添加新元素时用add

原文地址:https://www.cnblogs.com/eleni/p/11322354.html