英文词频统计预备,组合数据类型练习
2017-09-22 11:44 082何晓琳 阅读(156) 评论(0) 编辑 收藏 举报1.实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。
s='''Hey Jude, don't make it bad. Take a sad song and make it better. Remember to let her into your heart, Then you can start to make it better. Hey Jude, don't be afraid. You were made to go out and get her. The minute you let her under your skin, Then you begin to make it better. And anytime you feel the pain, hey Jude, refrain, Don't carry the world upon your shoulders. For well you know that it's a fool who plays it cool By making his world a little colder. Hey Jude, don't let me down. You have found her, now go and get her. Remember to let her into your heart, Then you can start to make it better. So let it out and let it in, hey Jude, begin, You're waiting for someone to perform with. And don't you know that it's just you, hey Jude, you'll do, The movement you need is on your shoulder. Hey Jude, don't make it bad. Take a sad song and make it better. Remember to let her under your skin, Then you'll begin to make it Better better better better better better, oh. Na na na, na na na na, na na na, hey Jude Na na na, na na na na, na na na, hey Jude Na na na, na na na na, na na na, hey Jude''' s.lower s.count('na') s.replace(',',' ') s.split(' ')
2.列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
>>>score=list('123212221333') ['1', '2', '3', '2', '1', '2', '2', '2', '1', '3', '3', '3'] >>>print(score.index('3')) 2 >>>for i in range(len(score)): score[i]=int(score[i]) >>> score [1, 2, 3, 2, 1, 2, 2, 2, 1, 3, 3, 3] >>> print(score.count('1')) 0 >>> print(score.count(1)) 3 >>> print(score.count(3)) 4 >>> score[0]='2' >>> print(score) ['2', 2, 3, 2, 1, 2, 2, 2, 1, 3, 3, 3]
3.简要描述列表与元组的异同
元组是不可变的,不可以进行添加、删除或修改,形式(1,2,3)
列表是可变的,可以进行添加、查询或修改的操作,形式[1,2,3]