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

时间:2023-02-14 17:29:50

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

 

s='''When I am down and oh my soul so weary
When troubles come
and my heart burdened be
Then I am still and wait here in the silence
Until you come and sit
a while with me.
You raise me up, so I can stand on mountains
You raise me up, to
walk on stormy seas
I am strong, when I am on your shoulders
You raise me up...
to more than I can be
You raise me upso
I can stand on mountains
You raise me up to walk on stormy seas
I am strong, when
I am on your shoulders
You raise me up...
to more than I can be
You raise me up, so
I can stand on mountains
You raise me up
to walk on stormy seas
I am strong, when
I am on your shoulders
You raise me up...
to more than I can be
You raise me up, so
I can stand on mountains
You raise me up, to walk on stormy seas
I am strong, when
I am on your shoulders
You raise me up...
to more than I can be
You raise me up...
to more than I can be '''

s=s.lower()
for i in ',.?':
    s=s.replace(i,' ')
print(s.split(' '))
print(s.count('talk'))
print(s.count('anymore'))

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

 

 

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

lb = list('1324864532')
print(lb)
for i in range(len(lb)):
lb[i]=int(lb[i])
print(lb.index(3))
print(lb.count(1))
print(lb.count(3))
lb.insert(1,9)
lb.pop(2)
lb.sort()
print(lb)

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

 

 

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

 

列表里面可存储有序项目,且列表中的数据是可变的,可增删改插,同时列表是可以嵌套的。

 

元组里面的数据是不可变的,只可增,不可删改,同时元组也是可以嵌套的。