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

9.22

时间:2017-09-22 14:05:46      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:分享   style   通过   获取   sel   let   不能   call   分片   

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

#字典实例:建立学生学号成绩字典,做增删改查遍历操作。
cj={39:89,33:90,38:80,40:79}
cj[36]=78#增加
print(cj)
cj.pop(39)#删除
print(cj)
cj[40]=69#修改值
print(cj)
print(cj.get(33,不存在))#查找,存在
print(cj.get(45,不存在))#查找,不存在

技术分享

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

#列表,元组,字典,集合的遍历。
ls=list(2376527)
print(遍历列表:)
for i in ls:
    print(i)
tp=tuple(76352526)
print(遍历元组:)
for i in tp:
    print(i)
dt={a:1,b:2,c:3,d:4}
print(遍历字典:)
for i in dt:
    print(i)
s=set(3242324)
print(遍历集合:)
for i in s:
    print(i)

技术分享

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

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

2.列表是以方括号形式表示,元组是以圆括号表示,字典以花括号表示,集合则是以[()]的形式表示

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

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

5.集合没有特殊的表示方法,而是通过一个set函数转换成集合。集合是一个无序不重复元素集,基本功能包括关系测试和消除重复元素.。

6.字典最大的价值是查询,通过键,查找值。

 

4.英文词频统计实例

待分析字符串

分解提取单词

大小写 txt.lower()

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

单词列表

单词计数字典

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, )#替换所有的,?
word=sorry.split( )#以‘ ’断开分成单独的字符串
#单词计数字典
dt={}#定义一个空字典
keys=set(word)#取键值
for i in keys:
    dt[i]=word.count(i)#dt[i]输出key
print(dt)

9.22

标签:分享   style   通过   获取   sel   let   不能   call   分片   

原文地址:http://www.cnblogs.com/liminghui3/p/7573860.html

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