组合数据类型练习,英文词频统计实例
列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
s=list('2133214423') print('作业评分列表',s) s.append('3') print('增加',s) s.pop() print('删除最后一个',s) s[5]='3' print('将第三个改为5',s) print('第一次出现3分的下标',s.index('3')) print('1分的同学有:',s.count('1')) print('3分的同学有:',s.count('3'))
字典实例:建立学生学号成绩字典,做增删改查遍历操作。
x={'11':'77','12':'57','13':'90'} x['14']=67 x.pop('12') x['11']=54print('查找2016学号成绩',x.get('15','无数据'))
列表,元组,字典,集合的遍历。
l=list('1233332121') t=tuple('654321123456') x={'201420':'88','201721':'90','201422':'78'} s=set('123321332122') print("列表:",l) for i in l: print(i,end=' ') print("\n") print("元组:",t) for i in t: print(i,end=' ') print("\n") print("字典:",x) for i in x: print(i,end='\t') print(x[i],end='\n') print("集合:",) for i in s: print(i,end=' ')
英文词频统计实例
new='''There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do. May you have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy? Always put yourself in others’shoes.If you feel that it hurts you,it probably hurts the other person, too. The happiest of people don’t necessarily have the best of everything;they just make the most of everything that comes along their way.Happiness lies for those who cry,those who hurt, those who have searched,and those who have tried,for only they can appreciate the importance of people who have touched their lives.Love begins with a smile,grows with a kiss and ends with a tear.The brightest future will always be based on a forgotten past, you can’t go on well in lifeuntil you let go of your past failures and heartaches. When you were born,you were crying and everyone around you was smiling.Live your life so that when you die,you're the one who is smiling and everyone around you is crying. Please send this message to those people who mean something to you,to those who have touched your life in one way or another,to those who make you smile when you really need it,to those that make you see the brighter side of things when you are really down,to those who you want to let them know that you appreciate their friendship.And if you don’t, don’t worry,nothing bad will happen to you,you will just miss out on the opportunity to brighten someone’s day with this message.''' new=new.lower() new=new.replace(',',' ') new=new.replace('.',' ') new=new.replace('?',' ') word=new.split() keys=set(word) zu={} no={'to','the'} for y in no: keys.remove(y) for i in keys: zu[i]=word.count(i) xu=list(zu.items()) xu.sort(key=lambda x:x[1],reverse=True) for x in range(10): print(xu[x])
浙公网安备 33010602011771号