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


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

    dict={"01":"90","02":"91","03":"92","04":"94","05":"95","06":"96","07":"97","08":"98"}
    print("查询02学号的成绩")
    print(dict.get('02'))
    print("删除07号的记录")
    dict.pop('07')
    print(dict.items())
    print("将01的成绩改为100")
    dict['01']=100
    print(dict.items())
    print("添加18号学生")
    dict["18"]="100"
    print(dict.items())
    print("遍历:")
    for i in dict:
        print(i)
    

    运行:

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

    lis=["01","02","03","04"]
    tu=("01","02","03","04","05")
    dic={"01":"91","02":"92","03":"93","04":"94"}
    s=set(lis)
    for i in lis:
        print(i)
    
    
    for i in tu:
        print(i)
    
    for i in dic:
       print('{0:<}:{1:>}'.format(i,dic[i]))
    
    for i in s:
        print(i)
    

    运行:

    答:列表是有序的元素集合,元组与列表很相像,但是最大的区别就是元组只能被读取而不能被修改。字典是以键值对作为单位来进行存放的集合。集合(set)则是一组key的无序集合,但是它只包含key,key值不能够重复,并且需要先创建一个List作为输入集合才能继续创建set。

    3英文词频统计实例

    bad='''your butt is mine
    
    I Gonna tell you right
    
    Just show your face
    
    In broad daylight
    
    I'm telling you
    
    On how I feel
    
    Gonna Hurt Your Mind
    
    Don't shoot to kill
    
    Shamone
    
    Shamone
    
    Lay it on me
    
    All right
    
    I'm giving you
    
    On count of three
    
    To show your stuff
    
    Or let it be
    
    I'm telling you
    
    Just watch your mouth
    I know your game
    
    What you're about
    
    Well they say the sky's the limit
    
    And to me that's really true
    
    But my friend you have seen nothin'
    
    Just wait till I get through
    
    Because I'm bad,I'm bad
    
    shamone
    
    (Bad,bad,really,really bad)
    
    You know I'm bad,I'm bad
    
    (Bad,bad,really,really bad)
    
    You know it
    
    You know I'm bad,I'm bad
    
    Come on,you know
    
    (Bad,bad,really,really bad)
    
    And the whole world
    
    Has to answer right now
    
    Just to tell you once again
    
    Who's bad
    
    The word is out
    
    You're doin' wrong
    
    Gonna lock you up
    
    Before too long
    
    Your lyin' eyes
    
    Gonna tell you right
    
    So listen up
    
    Don't make a fight
    
    Your talk is cheap
    
    You're not a man
    
    Your throwin' stones
    
    To hide your hands
    
    Well they say the sky's the limit
    
    And to me that's really true
    
    But my friend you have seen nothin'
    
    Just wait till I get through
    
    Because I'm bad,I'm bad
    
    shamone
    
    (Bad,bad,really,really bad)
    
    You know I'm bad,I'm bad
    
    (Bad,bad,really,really bad)
    
    You know it
    
    You know I'm bad,I'm bad
    
    Come on,you know
    
    (Bad,bad,really,really bad)
    
    And the whole world
    
    Has to answer right now
    
    Just to tell you once again
    
    Who's bad
    
    We could change the world tomorrow
    
    This could be a better place
    
    If you don't like what I'm sayin'
    
    Then won't you slap my face
    
    Because I'm bad'''
    
    bad=bad.lower()
    for i in ",.?!()":
        bad=bad.replace(i,' ')
    bad=bad.replace('
    ',' ')
    words=bad.split(' ')
    
    s=set(words)
    dic={}
    
    lis=[]
    value=[]
    for i in s:
        if(i==" "):
            continue
        if(i==""):
            continue  
        dic[i]=words.count(i)
        lis.append(words.count(i))
        value=dic.values()
    
    lis=list (dic.items())
    lis.sort(key=lambda x:x[1],reverse=True)
    for i in range(10):
        print(lis[i])
    

     

    运行:

     

      

  • 相关阅读:
    android studio 开发环境配置
    解决 php-cgi 启动时提示缺少 msvcr110.dll 的问题
    PHP5.5 + IIS + Win7的配置
    PHP版本VC6和VC9、Non Thread Safe和Thread Safe的区别
    [6]Telerik TreeView 复选框
    详解.NET IL代码(一)
    第三章 续:时间控件(TimePicker)
    第二章 时间控件(DateTime Picker)
    jquery常用方法
    RESTClient
  • 原文地址:https://www.cnblogs.com/mavenlon/p/7573333.html
Copyright © 2020-2023  润新知