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

本文介绍了使用Python进行文本处理的方法,包括清洗、统计单词频率等,并演示了如何通过列表进行评分统计的操作。同时对比了列表与元组的区别。

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

a='''Twinkle, twinkle, little star,
How I wonder what you are!
Up above the world so high,
Like a diamond in the sky.
Twinkle, twinkle, little star,
How I wonder what you are!

When the blazing sun is gone,
When he nothing shines upon,
Then you show your little light,
Twinkle, twinkle, all the night.
Twinkle, twinkle, little star,
How I wonder what you are!

Then the traveler in the dark
Thanks you for your tiny spark;
He could not see which way to go,
If you did not twinkle so.
Twinkle, twinkle, little star,
How I wonder what you are!

In the dark blue sky you keep,
And often through my curtains peep,
For you never shut your eye
Till the sun is in the sky.
Twinkle, twinkle, little star,
How I wonder what you are!

As your bright and tiny spark
Lights the traveler in the dark,
Through I know not what you are,
Twinkle, twinkle, little star.
Twinkle, twinkle, little star,
How I wonder what you are!'''
a=a.lower()
for i in ',.!?':
a=a.replace(i,' ')
words=a.split(' ')
print(words)

 

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

li=list('311123213332')
print(li)
for i in range(len(li)):
    li[i]=int(li[i])
print(li)
#排序
li.sort()
print(li)
#做增加一个1分的
li.append('1')
print(li)
#做删除第4个
li.pop(3)
print(li)
#做修改第2个
li[1]='2'
print(li)
#做查询第3个
print(li[2])
#计算有多少个一分的
print(li.count(1))
#计算有多少个三分的
print(li.count(3))

 

 

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

在列表中,你可以添加、删除或是搜索列表中的项目。
由于你可以增加或删除项目,所以列表是可变的数据类型,
即这种类型是可以被改变的。

元组和列表十分类似,但是元组是不可变的.
也就是说你不能修改元组。
元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,
即被使用的元组的值不会改变。

转载于:https://www.cnblogs.com/honghui/p/7561992.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值