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)

  • 相关阅读:
    扫面线模板
    (动态规划、栈)leetcode 84. Largest Rectangle in Histogram, 85. Maximal Rectangle
    tmux 常见命令汇总
    leetcode 221
    leetcode 319 29
    (贪心)leetcode 392. Is Subsequence, 771. Jewels and Stones, 463. Island Perimeter
    leetcode 982 668
    Python import 同文件夹下的py文件的函数,pycharm报错
    Windows里Anaconda-Navigator无法打开的解决方案
    Windows下 gpu版 Tensorflow 安装
  • 原文地址:https://www.cnblogs.com/888abc/p/7573350.html
Copyright © 2011-2022 走看看