zoukankan      html  css  js  c++  java
  • 9.22

    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)
  • 相关阅读:
    安卓学习第一课——电话拨号器
    CodeForces 644B【模拟】
    hdu5861【线段树】
    CodeForces 41A+43A【课上无聊刷水题系列】
    hdoj5493【树状数组+二分】
    HDU5894【组合数学】
    Codeforces643A【一种暴力】
    CodeForces 689C【二分】
    CodeForces 665B 【水-暴力】
    CodeForces 653A【水】
  • 原文地址:https://www.cnblogs.com/liminghui3/p/7573860.html
Copyright © 2011-2022 走看看