英文词频统计预备,组合数据类型练习

Python编程实例
本文通过具体实例介绍了使用Python进行文本处理的方法,包括歌词中特定字符的替换及单词计数,以及基于字符串创建评分列表并实现增删改查等功能。此外,还对比了列表与元组的区别。

1、实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。

song='''Hello, how are you?
It's so typical of me to talk about myself
I'm sorry, I hope that you're well
Did you ever make it out of that town
Where nothing ever happened?
Hello, from the other side
I must've called a thousand times to tell you
I'm sorry, for everything that I've done
But when I call you never seem to be home'''
song=song.lower()
print(song)

for i in song:
song1=song.replace(',',' ')
song2=song1.replace('?',' ')
print(song2)

songs=song2.split(' ')
print(songs)

print(songs.count('hello'))
print(songs.count('you'))

  

2、列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。

 

grade=list('13231313212212312')

grade.append('3')

grade.pop()

grade[3]='1'


print(grade[5])


for i in range (len(grade)):
grade[i]=int(grade[i])


for i in range (len(grade)):
if grade[i]==2:
print(i)
break

print('1分的同学有{0}个'.format(grade.count(1)))
print('3分的同学有{0}个'.format(grade.count(3)))

 

3、简要描述列表与元组的异同。

元组和列表十分类似,只不过元组和字符串一样是不可变的即你不能修改元组;而列表是可以修改的。

 

转载于:https://www.cnblogs.com/silu666-666/p/7562086.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值