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

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

时间:2017-09-22 17:45:22      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:mis   down   make   person   分解   代码   com   die   miss   

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

技术分享

a={‘01‘:‘80‘,‘02‘:‘70‘,‘03‘:‘100‘,‘04‘:‘90‘}
>>> a.get(‘04‘)
‘90‘
>>> a
{‘01‘: ‘80‘, ‘02‘: ‘70‘, ‘03‘: ‘100‘, ‘04‘: ‘90‘}
>>> a[‘05‘]=99
>>> a
{‘01‘: ‘80‘, ‘02‘: ‘70‘, ‘03‘: ‘100‘, ‘04‘: ‘90‘, ‘05‘: 99}
>>> del(a[‘05‘])
>>> a
{‘01‘: ‘80‘, ‘02‘: ‘70‘, ‘03‘: ‘100‘, ‘04‘: ‘90‘}
>>> a[‘01‘]=88
>>> a
{‘01‘: 88, ‘02‘: ‘70‘, ‘03‘: ‘100‘, ‘04‘: ‘90‘}

技术分享

2.列表,元组,字典,集合的遍历。

技术分享
列表
list=[‘1‘,‘2‘,‘3‘,‘2‘,‘1‘,‘5‘,‘5‘]
for i in (list):
    print(i)    
1
2
3
2
1
5
5
元组
tuple=(‘1‘,‘2‘,‘3‘,‘2‘,‘1‘,‘5‘,‘5‘)
>>> for i in (tuple):
    print(i)
    
1
2
3
2
1
5
5
字典
a={‘01‘:‘80‘,‘02‘:‘70‘,‘03‘:‘100‘,‘04‘:‘90‘}
>>> for i in (a):
    print(i)

    
01
02
03
04
>>> for i in (a):
    print(i,a[i])

    
01 80
02 70
03 100
04 90
集合
s=set([1,2,2,3])
>>> s
{1, 2, 3}
for i in (s):
    print(i)

    
1
2
3
技术分享


3.总结列表,元组,字典,集合的联系与区别。

列表和元组的数据都是有序的,且没有长度限制,元素类型可以不同,而字典和集合是没有序的,集需要提供一个list作为输入集合,字典和集合列表是用[ ],元组用(),字典和集合都是用{ },元组不能进行增删改,而列表,字典,集合都可以进行增删改操作,字典相对于列表查找插入速度快,不随着key增加变慢,但需要占用的内存大,而列表则相反,因此字典是用空间换取时间的一种方法。

4.英文词频统计实例

1)待分析字符串

2)分解提取单词

1))大小写 txt.lower()

2))分隔符‘.,:;?!-_’

3))单词列表

5.单词计数字典

技术分享
yingyu=‘‘‘There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do.

  May you have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy? Always put yourself in others’shoes.If you feel that it hurts you,it probably hurts the other person, too.

  The happiest of people don’t necessarily have the best of everything;they just make the most of everything that comes along their way.Happiness lies for those who cry,those who hurt, those who have searched,and those who have tried,for only they can appreciate the importance of people

  who have touched their lives.Love begins with a smile,grows with a kiss and ends with a tear.The brightest future will always be based on a forgotten past, you can’t go on well in lifeuntil you let go of your past failures and heartaches.

  When you were born,you were crying and everyone around you was smiling.Live your life so that when you die,you‘re the one who is smiling and everyone around you is crying.

  Please send this message to those people who mean something to you,to those who have touched your life in one way or another,to those who make you smile when you really need it,to those that make you see the brighter side of things when you are really down,to those who you want to let them know that you appreciate their friendship.And if you don’t, don’t worry,nothing bad will happen to you,you will just miss out on the opportunity to brighten someone’s day with this message.‘‘‘
yingyu=yingyu.lower()
for i in ‘,.!?‘:
    yingyu=yingyu.replace(i,‘ ‘)

print(yingyu)    
    
yingyu.count(‘who‘)

who=yingyu.split(‘ ‘)

words=set(yingyu)
>>> dt={ }

dt["to"]=yingyu.count("to")
>>> for i in words:
dt[i]=yingyu.count(i)

>>> for i in dt:
print("{0:<10}{1}".format(i,dt[i]))

技术分享

 技术分享

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

标签:mis   down   make   person   分解   代码   com   die   miss   

原文地址:http://www.cnblogs.com/D0204/p/7575705.html

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