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

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

时间:2017-09-22 15:22:58      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:images   span   for   can   一个   区别   运行   关系   功能   

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

 

d={‘10‘:80,‘11‘:87,‘13‘:76,‘13‘:50,‘14‘:89,‘15‘:96,‘16‘:89,‘17‘:100}
d[‘40‘]=95#增加学号为40
print(d)
d.pop(‘17‘)#删除学号为17的
print(d)
d[‘11‘]=60#修改学号为11的
print(d)
d.get(‘15‘)
print(d)

运行结果:技术分享

 

 

 

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

lis = list(‘10086‘)

tup = tuple(‘10010‘)

d = dict(zip([1,2,3,4],[7,8,9,10]))
s = set(lis)

print(‘遍历列表:‘)
for i in lis:
    print(i)

print(‘遍历元组:‘)
for i in tup:
     print(i)

print(‘遍历字典:‘)
for i in d:
     print(i)

print(‘遍历集合:‘)
for i in s:
     print(i)

运行结果

技术分享

1.列表,元组,字典是有顺序的,而集合是没顺序的

2.列表是以方括号形式表示,元组是以圆括号表示,字典以花括号表示,集合则是以[()]的形式表示,是一个无序不重复元素集,基本功能包括关系测试和消除重复元素

3.列表是可变对象,它支持在原处修改的操作.也可以通过指定的索引和分片获取元素。区别于元组,可动态增加,删除,更新。

4.元组和列表在结构上没有什么区别,唯一的差异在于元组是只读的,不能修改。元组用“()”表示。元组一旦定义其长度和内容都是固定的。一旦创建元组,则这个元组就不能被修改,即不能对元组进行更新、增加、删除操作。若想创建包含一个元素的元组,则必须在该元素后面加逗号“,”,否则创建的不是一个元组,而是一个字符串。

 

3.英文词频统计实例

   A.待分析字符串

    B.分解提取单词

              a.大小写 txt.lower()

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

               c.单词列表

C单词计数字典

sorry=‘‘‘You gotta go and get angry at all of my honesty
You know I try but I don’t do too well with apologies
I hope I don’t run out of time, could someone call a referee?
Cause I just need one more shot at forgiveness
I know you know that I made those mistakes maybe once or twice
By once or twice I mean maybe a couple a hundred times
So let me, oh let me redeem, oh redeem, oh myself tonight
Cause I just need one more shot at second chances
Yeah, is it too late now to say sorry?
Cause I’m missing more than just your body
Is it too late now to say sorry?
Yeah I know that I let you down
Is it too late to say I’m sorry now?
I’m sorry, yeah
Sorry, yeah
Sorry
Yeah I know that I let you down
Is it too late to say sorry now?
I’ll take every single piece of the blame if you want me to
But you know that there is no innocent one in this game for two
I’ll go, I’ll go and then you go, you go out and spill the truth
Can we both say the words and forget this?
Is it too late now to say sorry?
Cause I’m missing more than just your body
Is it too late now to say sorry?
Yeah I know that I let you down
Is it too late to say I’m sorry now?
I’m not just trying to get you back on me
Cause I’m missing more than just your body
Is it too late now to say sorry?
Yeah I know that I let you down
Is it too late to say sorry now?
I’m sorry, yeah
Sorry, oh
Sorry
Yeah I know that I let you down
Is it too late to say sorry now?
I’m sorry, yeah
Sorry, oh
Sorry
Yeah I know that I let you down
Is it too late to say sorry now?‘‘‘
sorry=sorry.lower()
for i in ‘?,‘:
    sorry=sorry.replace(i,‘ ‘)#全部小写
    words=sorry.split(‘ ‘)#以空格分隔
     print(words)

dic={}#定义一个空字典
words.sort()#排列切片好的单词
d=set(words)#集合d的元素就是切片好的单词

for i in d:
    dic[i]=0#循环插入值为空的主键i

for i in words:
    dic[i]=dic[i]+1#利用循环计算主键个数
print(dic)

技术分享

 

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

标签:images   span   for   can   一个   区别   运行   关系   功能   

原文地址:http://www.cnblogs.com/zhuyinyinyin/p/7573315.html

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