英文词频统计预备,组合数据类型练习
1、实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。
lyric='''Just one last dance....oh baby...just one last dance We meet in the night in the Spanish café I look in your eyes just don't know what to say It feels like I'm drowning in salty water A few hours left 'til the sun's gonna rise tomorrow will come an it's time to realize our love has finished forever how I wish to come with you (wish to come with you) how I wish we make it through Just one last dance before we say goodbye when we sway and turn round and round and round it's like the first time Just one more chance hold me tight and keep me warm cause the night is getting cold and I don't know where I belong Just one last dance The wine and the lights and the Spanish guitar I'll never forget how romantic they are but I know, tomorrow I'll lose the one I love There's no way to come with you it's the only thing to do Just one last dance before we say goodbye when we sway and turn round and round and round it's like the first time Just one more chance hold me tight and keep me warm cause the night is getting cold and I don't know where I belong Just one last dance, just one more chance, just one last dance ''' lyric=lyric.lower() for i in ',.?-_': lyric=lyric.replace(i,' ') words=lyric.split() print(words) print ('dance:{0}'.format(lyric.count('dance'))) print ('round:{0}'.format(lyric.count('round')))

2 列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
grade=list('19921127') for i in range(len(grade)): grade[i]=int(grade[i]) print('遍历:',grade) grade.insert(4,5) print('在第四位增加一个‘5’:',grade) grade.pop(5) print('取第五个数:',grade) print('取下标为2的数:',grade[2]) print('第一个9分的下标为:',grade.index(9)) print('1分的同学有:',grade.count(1)) print('2分的同学有:',grade.count(2))

3、简要描述列表与元组的异同。
列表
list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目。列表中的项目。列表中的项目应该包括在方括号中,这样python就知道你是在指明一个列表。一旦你创建了一个列表,你就可以添加,删除,或者是搜索列表中的项目。由于你可以增加或删除项目,我们说列表是可变的数据类型,即这种类型是可以被改变的。
列表是可以嵌套的。
元组
元祖和列表十分相似,不过元组是不可变的。即你不能修改元组。元组通过圆括号中用逗号分隔的项目定义。
元组通常用在使语句或用户定义的函数能够安全的采用一组值的时候,即被使用的元组的值不会改变。元组可以嵌套。

浙公网安备 33010602011771号