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

    实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。
    例如,查询第一个3分的下标
    统计1分的同学有多少个,3分的同学有多少个

    >>> ap=list("0213023021321320132132131231")
    >>> ap
    ['0', '2', '1', '3', '0', '2', '3', '0', '2', '1', '3', '2', '1', '3', '2', '0', '1', '3', '2', '1', '3', '2', '1', '3', '1', '2', '3', '1']
    >>> ap.append('4')
    >>> ap
    ['0', '2', '1', '3', '0', '2', '3', '0', '2', '1', '3', '2', '1', '3', '2', '0', '1', '3', '2', '1', '3', '2', '1', '3', '1', '2', '3', '1', '4']
    >>> ap.pop(2)
    '1'
    >>> ap
    ['0', '2', '3', '0', '2', '3', '0', '2', '1', '3', '2', '1', '3', '2', '0', '1', '3', '2', '1', '3', '2', '1', '3', '1', '2', '3', '1', '4']
    >>> ap.index('3')
    2
    >>> ap.count('1')
    7
    >>> ap.count('3')
    8
    >>> 

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

    >>> d={"1":"李家秀","29":"李韶豪","18":"刘卓晖","30":"曾国建"}
    >>> print(d)
    {'1': '李家秀', '29': '李韶豪', '18': '刘卓晖', '30': '曾国建'}
    >>> d["4"]="吴敏敏"
    >>> print(d)
    {'1': '李家秀', '29': '李韶豪', '18': '刘卓晖', '30': '曾国建', '4': '吴敏敏'}
    >>> d["29"]
    '李韶豪'
    >>> del(d['1'])
    >>> d
    {'29': '李韶豪', '18': '刘卓晖', '30': '曾国建', '4': '吴敏敏'}'
    >>> d.values()
    dict_values(['李韶豪', '刘卓晖', '曾国建', '吴敏敏'])
    >>> d.items()
    dict_items([('29', '李韶豪'), ('18', '刘卓晖'), ('30', '曾国建'), ('4', '吴敏敏')])
    >>> d.get('30','ll')
    '曾国建'
    >>> d
    {'29': '李韶豪', '18': '刘卓晖', '30': '曾国建', '4': '吴敏敏'}
    >>> d.pop('30','ll')
    '曾国建'
    >>> d
    {'29': '李韶豪', '18': '刘卓晖', '4': '吴敏敏'}
    >>> 

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

    >>> a=list("132132002031323")
    >>> a
    ['1', '3', '2', '1', '3', '2', '0', '0', '2', '0', '3', '1', '3', '2', '3']
    >>> b=tuple("1321320321")
    >>> b
    ('1', '3', '2', '1', '3', '2', '0', '3', '2', '1')
    >>> c = {'apple':1, 'banana':2, 'orange':3}
    >>> c
    {'apple': 1, 'banana': 2, 'orange': 3}
    >>> d=set("23132321321320001")
    >>> d
    {'2', '3', '0', '1'}
    >>> for i in a:
        print(i)
    
        
    1
    3
    2
    1
    3
    2
    0
    0
    2
    0
    3
    1
    3
    2
    3
    >>> for i in b:
        print(i)
    
        
    1
    3
    2
    1
    3
    2
    0
    3
    2
    1
    >>> for i in c:
        print(i,c[i])
    
        
    apple 1
    banana 2
    orange 3
    >>> for i in d:
        print(i)
    
        
    2
    3
    0
    1
    >>> 

    英文词频统计实例

    1. 待分析字符串
    2. 分解提取单词
      1. 大小写 txt.lower()
      2. 分隔符'.,:;?!-_’
    3. 计数字典
    4. 排序list.sort()
    5. 输出TOP(10)
    sr='''Just one last dance ,oh baby just one last dance!
    We meet in the night in the Spanish café,
    I look in your eyes just don't know what to say,
    It feels like I'm drowning in salty water,A few hours left 'til the sun's gonna rise,
    tomorrow will come an it's time to realize.our love has finished forever,
    how I wish to come with you!how I wish we make it through!
    Just one last dance!before we say goodbye,
    when we sway and turn round and round and round,
    it's like the first time,Just one more chance,
    hold me tight and keep me warm.cause the night is getting cold.
    and I don't know where I belong,Just one last dance! '''
    
    news =sr.lower()
    for i in ',.':
        news=news.replace(i,' ')
    words=news.split(' ')
    print(words)
    dic={}
    keys=set(words)
    for i in keys:
        dic[i]=words.count(i)
    wc=list(dic.items())
    wc.sort(key = lambda x:x[1],reverse=True)
    for i in range(10):
        print(wc[i])

  • 相关阅读:
    linux hosts文件详+mac主机名被莫名其妙修改
    WPF整理--动态绑定到Logical Resource
    WPF整理-使用逻辑资源
    WPF整理-自定义一个扩展标记(custom markup extension)
    WPF整理-XAML访问静态属性
    WPF整理-为控件添加自定义附加属性
    WPF整理-为User Control添加依赖属性
    使用MS Test进行单元测试
    WPF整理-XAML构建后台类对象
    毕业那点事儿--回顾在大学这7年
  • 原文地址:https://www.cnblogs.com/toronad/p/7560659.html
Copyright © 2011-2022 走看看