zoukankan      html  css  js  c++  java
  • 作业8——组合数据类型练习,英文词频统计实例上

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

    d={'2015001':50,'2015002':85,'2015003':75,'2015004':78,'2015005':56}
    print('2015001的值:',d['2015001'])
    print('键:',d.keys())
    print('值:',d.values())
    print('get函数:',d.get('2015013'))
    print(d.get('2015013','O(∩_∩)O'))
    print('pop弹出函数:',d.pop('2015005'))
    #增加'201599':999#
    d['2015999']=999
    #修改‘2015004’#
    d['2015004']=888
    del d['2015001']
    print(d)
    
    for i in d:
        print('遍历:',i,d[i])

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

    ls=[1,2,3,4,5,6]
    tu=('q','w','e','t')
    d={'1':1,'p':2,'u':2}
    s=set([7,8,9,10])
    
    for i in ls:
        print('list:',i)
    
    for i in tu:
        print('tuple:',i)
    
    for i in d:
        print('dict:',i,d[i])
    
    for i in s:
        print('set:',i)

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

                1)list是一种有序的序列,正向递增、反向递减序号。可以随时添加和删除其中的元素,没                   有长度限制,元素类型可以不同。

                2)tuple和list非常类似,都是有序的序列。但是tuple一旦初始化就不能修改。

                3)dict使用键-值(key-value)存储,key是不可变的对象。插入和查找速度快,不会随着                   key的增加而变慢,需要占用大量的内存。dict是用空间换取时间的一种方法。

    3、英文词频统计实例

                     a.待分析字符串

                     b.分解提取单词

                             a.大小写 txt.lower()

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

                             c.单词列表

                     c.单词计数字典

    abc='''Models from different countries took to the catwalk in
    qipao, a traditional Chinese dress, at the first Shenyang Qipao
    International Cultural Festival held at the Shenyang Palace Museum
    in Shenyang, Northwest China's Liaoning province, on Tuesday.'''
    
    abc=abc.lower()
    for i in ',.':
        abc=abc.replace(i,' ')
    qwe=abc.split()
    
    #定义字典#
    d={}
    
    #写入字典#
    di=set(qwe)
    for i in di:
        d[i]=0
    for i in qwe:
        d[i]=d[i]+1
    it=d.items()
    print(it)

  • 相关阅读:
    [GIT] warning: LF will be replaced by CRLF问题解决方法
    最近想学的工具
    如何在webstrom中配置eslint和less
    Git常用命令
    windows下nginx安装、配置与使用
    关于 addEventListener 和 handleEvent 方法
    PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toStr
    Git使用详细教程
    9个永恒的UI设计原则
    常见浏览器兼容性问题与解决方案
  • 原文地址:https://www.cnblogs.com/888abc/p/7573350.html
Copyright © 2011-2022 走看看