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

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)简要描述列表与元组的异同。

 

 

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

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

posted on 2017-09-22 08:21  133陈贝  阅读(99)  评论(0编辑  收藏  举报