博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
组合数据类型练习,英文词频统计实例上
阅读量:4595 次
发布时间:2019-06-09

本文共 2724 字,大约阅读时间需要 9 分钟。

 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)

 

转载于:https://www.cnblogs.com/zhuyinyinyin/p/7573315.html

你可能感兴趣的文章
Linux CentOS中防火墙的关闭及开启端口
查看>>
机器学习中的数学(1)-回归(regression)、梯度下降(gradient descent)
查看>>
网页性能优化
查看>>
destoon网站转移空间教程
查看>>
.Net 三款工作流引擎比较:WWF、netBPM 和 ccflow
查看>>
P1280 尼克的任务(DP)
查看>>
在PC上测试移动端网站和模拟手机浏览器的5大方法
查看>>
Saltstack_使用指南18_API
查看>>
javascript 之 浏览器宽度、高度总结
查看>>
python实例31[列出目录下所有的文件到txt]
查看>>
修复iPhone上submit按钮bug
查看>>
backbone collection add 事件回调参数
查看>>
转载:XGBOOST算法梳理
查看>>
EM算法
查看>>
istringstream。PKU2493 Rdeaalbe。
查看>>
linux监控系统的状态
查看>>
编码风格
查看>>
Linux的ls命令在Windows中的应用
查看>>
4.Spring注解+SpringMVC注解+MyBatis注解(动态sql)
查看>>
算法导论 CLRS 24.1-5 解答
查看>>