第3次作业(组合数据类型,英文词频统计 )
1.
#列表可重复,类型不同,用[]表示 listA = ['a', 'b', 'c', 1, 2] # 遍历list for item in listA: print(item) #元组是只读的,不能修改。元组用“()”表示 tuple1 = (1,2,'a',4,'5',6) for item in tuple1: print(item) #字典定义了键和值之间一对一的关系,但它们是以无序的方式储存的。 dict1 = {'name' : 'yeoman', 'age' : 24, 'sex' : 'Male'} for i in dict1: print(i,dict1[i]) #集合是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素.想要创建空集合,你必须使用 set() set1 = set('abcd') print(set1) for i in set1: print(i)
2.
str='''Hall Of Fame - speedmaster Written by:A. FRAMPTON/M. SHEEHAN/O DONOGHUE/BAR You can be the greatest you can be the best You can be the king kong bangin' on your chest You can beat the world you can beat the war You can talk to god go bangin' on his door You can throw your hands up you can beat the clock You can move a mountain you can break rocks You can be a master don't wait for luck Dedicate yourself and you gon' find yourself Standin' in the hall of fame And the world's gonna know your And the world's gonna know your name Cause you burn with the brightest flame You can go the distance you can run the mile You can walk straight through hell with a smile You can be the hero you can get the gold Breakin' all the records they thought never could be broke Do it for your people do it for your pride How are you ever gonna know if you never even try Do it for your country do it for your name Cause there's gon' be a day when you're Standin' in the hall of fame And the world's gonna know your name Cause you burn with the brightest flame And the world's gonna know your name And you'll be on the walls of the hall of fame Be a champion Be a champion Be a champion Be a champion Be students be teachers Be politicians be preachers Yeah Be believers be leaders Be astronauts be champions Be truth seekers Be students be teachers Be politicians be preachers Yeah Be believers be leaders Be astronauts be champions Standin' in the hall of fame And the world's gonna know your name Cause you burn with the brightest flame And the world's gonna know your name And you'll be on the walls of the hall of fame Be a champion You can be a champion Be a champion You can be a champion You can be a champion You can be a champion You can be a champion You can be a champion Standing in the hall of fame - ''' str = str.lower() #全部转为小写 sep = '.,:;?!' #删除特殊字符 for a in sep: str = str.replace(a,' ') #print(str) strlist = str.split() #分割字符 #print(len(strlist),strlist) strset = set(strlist) #将字符转为列表 #print(len(strset),strset) for word in strset: #查看单词出现次数 print(word,strlist.count(word)) se = {'a','the','and','we','you','of','si','s','ter','to'} #删除无语义词 strsete =strset-se #print(strsete) strdict = {} #单词计数字典 for word in strset: strdict[word] = strlist.count(word) print(len(strdict),strdict) wordlist = list(strdict.items()) wordlist.sort(key=lambda x:x[1],reverse=True) #用lambda函数排序 print(strlist) for i in range(20): #输出TOP(20) print(wordlist[i])