组合数据类型练习,英文词频统计实例

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

never = '''See I never thought that I could walk through fire
I never thought that I could take the burn
I never had the strength to take it higher
Until I reached the point of no return

And there's just no turning back
When your hearts under attack
Gonna give everything I have
It's my destiny

I will never say never! (I will fight)
I will fight till forever! (make it right)
Whenever you knock me down
I will not stay on the ground
Pick it up
Pick it up
Pick it up
Pick it up up up
And never say never

I never thought I could feel this power
I never thought that I could feel this free
I'm strong enough to climb the highest tower
And I'm fast enough to run across the sea

And there's just no turning back
When your hearts under attack
Gonna give everything I have
Cause this is my destiny

I will never say never!(I will fight)
I will fight till forever!(make it right)
Whenever you knock me down
I will not stay on the ground
Pick it up
Pick it up
Pick it up
Pick it up, up, up
And never say never

Here we go!
Guess who?
JSmith and Jb!
I gotcha lil bro
I can handle him
Hold up, aight?
I can handle him

Now he's bigger than me
Taller than me
And he's older than me
And stronger than me
And his arms a little bit longer than me
But he ain't on a JB song with me!

I be trying a chill
They be trying to side with the thrill
No pun intended, was raised by the power of Will

Like Luke with the force, when push comes to shove
Like Cobe with the 4th, ice water with blood

I gotta be the best, and yes
We're the flyest
Like David and Goliath
I conquered the giant
So now I got the world in my hand
I was born from two stars
So the moon's where I land

I will never say never!(I will fight)
I will fight till forever!(make it right)
Whenever you knock me down
I will not stay on the ground
Pick it up
Pick it up
Pick it up
Pick it up, up, up
And never say never

I will never say never!(I will fight)
I will fight till forever!(make it right)
Whenever you knock me down
I will not stay on the ground
Pick it up
Pick it up
Pick it up
Pick it up, up, up
And never say never
Never say never
Never say never
Never say never
Never say never
Never say never
Never say never
Never say never
And never say never'''
never=never.replace('?',' ')
never=never.replace('',' ')
never=never.replace(',',' ')
never=never.replace('.',' ')
never=never.replace('\'',' ')
never=never.lower()
print("never次数统计:",never.count('never'))
print("\n转换后歌词:\n",never)

结果:

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

score=list('21223113321')
print('作业评分列表:',score)
score.append('3')
print('增加:',score)
score.pop()
print('删除:',score)
score.insert(2,'1')
print('插入:',score)
score[2]='2'
print('修改:',score)
print('第一个3分的下标:',score.index('3'))
print('1分的个数:',score.count('1'))
print('3分的个数:',score.count('3'))

结果:

3、字典实例:建立学生学号成绩字典,做增删改查遍历操作。

d={'金珂':93,'高渐离':74,'鲁班':45,'五五开':66}
print('学生成绩字典:',d)
d['张飞']=92
print('增加:',d)
d.pop('五五开')
print('删除:',d)
d['金珂']=73
print('修改:',d)
print('查询高渐离成绩:',d.get('高渐离',''))

结果:

4、列表,元组,字典,集合的遍历。总结列表,元组,字典,集合的联系与区别。

ls=list("4613125646")
tu=tuple("4613125646")
s=set("4613125646")
d={'金珂':93,'高渐离':74,'鲁班':45,'五五开':66}
print("列表:",ls)
for i in ls:
    print(i,end=' ')
print("\n")
print("元组:",tu)
for i in tu:
    print(i,end=' ')
print("\n")
print("集合:",s)
for i in s:
    print(i,end=' ')
print("\n")
print("字典:",d)
for i in d:
    print(i,end='\t')
    print(d[i],end='\n')

结果:

5、英文词频统计实例

  1. 待分析字符串
  2. 分解提取单词
    1. 大小写 txt.lower()
    2. 分隔符'.,:;?!-_’
  3. 计数字典
  4. 排序list.sort()
  5. 输出TOP(10)
new = '''See I never thought that I could walk through fire
I never thought that I could take the burn
I never had the strength to take it higher
Until I reached the point of no return

And there's just no turning back
When your hearts under attack
Gonna give everything I have
It's my destiny

I will never say never! (I will fight)
I will fight till forever! (make it right)
Whenever you knock me down
I will not stay on the ground
Pick it up
Pick it up
Pick it up
Pick it up up up
And never say never

I never thought I could feel this power
I never thought that I could feel this free
I'm strong enough to climb the highest tower
And I'm fast enough to run across the sea

And there's just no turning back
When your hearts under attack
Gonna give everything I have
Cause this is my destiny

I will never say never!(I will fight)
I will fight till forever!(make it right)
Whenever you knock me down
I will not stay on the ground
Pick it up
Pick it up
Pick it up
Pick it up, up, up
And never say never

Here we go!
Guess who?
JSmith and Jb!
I gotcha lil bro
I can handle him
Hold up, aight?
I can handle him

Now he's bigger than me
Taller than me
And he's older than me
And stronger than me
And his arms a little bit longer than me
But he ain't on a JB song with me!

I be trying a chill
They be trying to side with the thrill
No pun intended, was raised by the power of Will

Like Luke with the force, when push comes to shove
Like Cobe with the 4th, ice water with blood

I gotta be the best, and yes
We're the flyest
Like David and Goliath
I conquered the giant
So now I got the world in my hand
I was born from two stars
So the moon's where I land

I will never say never!(I will fight)
I will fight till forever!(make it right)
Whenever you knock me down
I will not stay on the ground
Pick it up
Pick it up
Pick it up
Pick it up, up, up
And never say never

I will never say never!(I will fight)
I will fight till forever!(make it right)
Whenever you knock me down
I will not stay on the ground
Pick it up
Pick it up
Pick it up
Pick it up, up, up
And never say never
Never say never
Never say never
Never say never
Never say never
Never say never
Never say never
Never say never
And never say never'''

new=new.lower()
for i in ',.?!\n"':
    new=new.replace(i,' ')
words = new.split(' ') 
dic ={}
keys = set(words)
for i in keys:
    dic[i]=words.count(i)
tupleee = list(dic.items())
tupleee.sort(key= lambda x:x[1],reverse = True)
for i in range(10):
    print(tupleee[i])

结果:

posted @ 2017-09-21 16:02  谢瑞楷  阅读(182)  评论(0编辑  收藏  举报