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

本文通过两个实例展示了如何处理文本数据并进行简单的统计分析,包括替换字符、转换大小写、统计单词出现次数;同时介绍了如何从字符串创建列表,并进行一系列的操作如查询元素位置、统计特定元素的数量等。此外,还比较了列表与元组的区别。

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

复制代码
news = ''' China hopes Singapore will support Chinese enterprises who wish to participate in the Singapore-Malaysia high-speed railway project, Premier Li Keqiang said on Tuesday.
"China has cutting-edge, safe and reliable, cost-effective high-speed railway technology," Li said in his talks with visiting Singaporean Prime Minister Lee Hsien Loong.
Malaysia and Singapore have agreed to build a 360-km high-speed rail link between Singapore and Kuala Lumpur, which is expected to start operation by December 2026 and cut travel time to about 90 minutes.
Singapore welcomes Chinese businesses to the project, Lee said.'''

news=news.lower()
print(news.count('china'))
for i in ',.':
    news=news.replace(i,' ')
print(news)
words=news.split(' ')
print(words)
复制代码

结果如下所示:

 

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

复制代码
homework=list('123478526')
for i in range(len(homework)):
    homework[i]=int(homework[i])
print(homework)
print(homework.index(8))
print(homework.count(1))
print(homework.count(3))
复制代码

结果如下所示:

 

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

 

 

元组一旦被赋值不能被更改,而列表被赋值后可以更改。

列表和元组所用的符号不同,列表的用方括号,元组的用圆括号。

转载于:https://www.cnblogs.com/Betty18/p/7572806.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值