组合数据类型练习
一.有字符串创建一个评分表
s = list("3221212132212") print("评分表",s) s.append("4") print("增加",s) s.pop() print(s.count("3")) print(s.count("1")) print(s.index("3"))
二.建立学生学号成绩字典
d={39:100,40:99,41:98,42:98} print(d) print(d[39]) print(d.pop(41)) print(d.values())
三.英文歌曲词频统计
s=''' I\’m a big big girl ! In a big big world ! It\’s not a big big thing if U leave me. But I do do feel. That I too too will miss U much. Miss U much ! I can see the first leaf falling. It\’s all yellow & nice. It\’s so very cold outside. Like the way I\’m feeling inside. I\’m a big big girl ! In a big big world ! It\’s not a big big thing if U leave me. But I do do feel. That I too too will miss U much. Miss U much ! Outside it\’s now raining. And tears are falling from my eyes. I\’m a big big girl ! In a big big world ! It\’s not a big big thing if U leave me. But I do do feel. That I too too will miss U much. Miss U much ! I have Ur arms around me ooooh like fire. But when I open my eyes. U\’re gone ! I\’m a big big girl ! In a big big world ! It\’s not a big big thing if U leave me. But I do do feel. That I too too will miss U much. Miss U much ! I\’m a big big girl ! In a big big world ! It\’s not a big big thing if U leave me. But I do feel I will miss U much ! Miss U much ! ''' print("do出现次数",s.count('do')) s=s.replace(",","") s=s.replace('.','') s=s.replace('?','') s=s.replace('!','') s=s.replace('\n','') s=s.lower() s1=s.split(' ') key=set(s1) dic={} for i in key: dic[i]=s.count(i) wc=list(dic.items()) wc.sort(key=lambda x:x[1],reverse=True) for i in range(10): print(wc[i])
四..列表,元组,字典,集合的遍历
s=list('456132798') t=set('4561327489') c={'小明':'1','小红':'3','小华':'2'} x=(1, 2, 3, 4, 5 ) print('列表的遍历为:',end='') for i in s: print(i,end='') print() print('元祖的遍历为:',end='') for i in x: print(i,end='') print() print('字典key的遍历为:',end='') for i in c: print(i,end='') print() print('字典value的遍历为:',end='') for i in c.values(): print(i,end='') print() print('数组的遍历为:',end='') for i in x: print(i,end='')