码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-20 23:30:14      阅读:355      评论:0      收藏:0      [点我收藏+]

标签:win   class   字符   sha   下载   操作   歌词   常用   not   

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

     

str=‘‘‘Counting stars Lately I‘ve been, I‘ve been losing sleep
Dreaming ‘bout the things that we could be
But baby I‘ve been, I‘ve been prayin‘ hard
Said no more counting dollars
We‘ll be counting stars
Yeah, we‘ll be counting stars
I see this life Like a swinging vine
Swing my heart across the line
In my face is flashing signs
Seek it out and ye shall find
Old, but I‘m not that old
Young, but I‘m not that bold
And I don‘t think the world is sold
I‘m just doing what we‘re told
I, feel something so right
But doing the wrong thing
I, feel something so wrong
But doing the right thing‘‘‘

将所有,.?!等替换为空格

str = str.replace(‘,‘,‘ ‘)
str = str.replace(‘\n‘,‘‘)

将所有大写转换为小写
str = str.lower()

分隔出一个一个的单词
words = str.split(‘ ‘)
print(str)

统计某几个单词出现的次数
print (‘i:{0}‘.format(str.count(‘i‘)))
print (‘we:{0}‘.format(str.count(‘we‘)))
print (‘be:{0}‘.format(str.count(‘be‘)))
print (‘so:{0}‘.format(str.count(‘so‘)))

运行结果:

counting stars lately i‘ve been i‘ve been losing sleepdreaming ‘bout the things that we could bebut baby i‘ve been i‘ve been prayin‘ hardsaid no more counting dollarswe‘ll be counting starsyeah we‘ll be counting starsi see this life like a swinging vineswing my heart across the linein my face is flashing signsseek it out and ye shall findold but i‘m not that oldyoung but i‘m not that boldand i don‘t think the world is soldi‘m just doing what we‘re toldi feel something so rightbut doing the wrong thingi feel something so wrongbut doing the right thing
i:45
we:4
be:7
so:5

 

 

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

homework=list(‘134343633‘)
for i in range(len(homework)):
homework[i]=int(homework[i])
print(homework)
print(homework.index(6))
print(homework.count(1))
print(homework.count(3))

运行结果:

[1, 3, 4, 3, 4, 3, 6, 3, 3]
6
1
5

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

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

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

标签:win   class   字符   sha   下载   操作   歌词   常用   not   

原文地址:http://www.cnblogs.com/nigongbin/p/7562043.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!