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

    
    
    1. 列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
    >>> gd=list('120123101312313121')
    >>> gd
    ['1', '2', '0', '1', '2', '3', '1', '0', '1', '3', '1', '2', '3', '1', '3', '1', '2', '1']
    >>> gd.append('4')
    >>> gd
    ['1', '2', '0', '1', '2', '3', '1', '0', '1', '3', '1', '2', '3', '1', '3', '1', '2', '1', '4']
    >>> gd.pop(1)
    '2'
    >>> gd
    ['1', '0', '1', '2', '3', '1', '0', '1', '3', '1', '2', '3', '1', '3', '1', '2', '1', '4']
    >>> gd.index('3')
    4
    >>> gd.count('1')
    8
    >>> gd.count('3')
    4

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

    >>> dic={'张三':'1','李四':'5','王五':'11','林六':'17','何七':'25'}
    >>> dic
    {'张三': '1', '李四': '5', '王五': '11', '林六': '17', '何七': '25'}
    >>> dic['潘二']='50'
    >>> dic
    {'张三': '1', '李四': '5', '王五': '11', '林六': '17', '何七': '25', '潘二': '50'}
    >>> del(dic['潘二'])
    >>> dic
    {'张三': '1', '李四': '5', '王五': '11', '林六': '17', '何七': '25'}
    >>> dic.values()
    dict_values(['1', '5', '11', '17', '25'])
    >>> dic.keys()
    dict_keys(['张三', '李四', '王五', '林六', '何七'])
    >>> dic.items()
    dict_items([('张三', '1'), ('李四', '5'), ('王五', '11'), ('林六', '17'), ('何七', '25')])
    >>> dic.get('李四')
    '5'
    >>> dic.pop('李四')
    '5'
    >>> dic
    {'张三': '1', '王五': '11', '林六': '17', '何七': '25'}

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

    a=list('1234456789123456789')
    >>> a
    ['1', '2', '3', '4', '4', '5', '6', '7', '8', '9', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    >>> b=tuple('123456789123456789')
    >>> b
    ('1', '2', '3', '4', '5', '6', '7', '8', '9', '1', '2', '3', '4', '5', '6', '7', '8', '9')
    >>> c={'qqq':'1','www':'2','eee':'3'}
    >>> c
    {'qqq': '1', 'www': '2', 'eee': '3'}
    >>> d=set('123456789123456789')
    >>> d
    {'9', '8', '3', '5', '1', '6', '7', '4', '2'}
    for i in a:
        print(i,end=' ')
    
    1 2 3 4 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 
    for i in b:
        print(i,end=' ')
        
    1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 
    for i in c:
        print(i,c[i],end=' ')
        
    qqq 1 www 2 eee 3 
    >>> for i in d:
        print(i,end=' ')
    
    9 8 3 5 1 6 7 4 2

    英文词频统计实例

    1. 待分析字符串
    2. 分解提取单词
      1. 大小写 txt.lower()
      2. 分隔符'.,:;?!-_’
    3. 计数字典
    4. 排序list.sort()
    5. 输出TOP(10)
    w='''I need you boo and I see you boo
    And the heart's all over the world tonight
    Said the heart's all over the world tonight
    Hey, little mama, ooh you're a winner
    Hey, little mama, yes you're a winner
    I'm so glad to be yours
    You're a class all your own and
    Ooh, little cutie, when you talk to me
    I swear the whole world stops
    You're my sweetheart
    I'm so glad that you're mine
    You are one of a kind and
    You mean to me
    What I mean to you and
    Together baby
    There is nothing we won't do
    'Cause if I got you
    I don't need money
    I don't need cars
    Girl, you're my all
    And oh, I'm into you
    Girl, no one else would do
    'Cause with every kiss and every hug
    You make me fall in love
    And now I know I can't be the only one
    I bet there's hearts all over'''
    for i in ",.":
        w=w.replace(i,",")
        for i in w:
            w=w.lower()
    words=w.split(" ")
    
    keys=set(words)
    
    dict={}
    
    for i in keys:
        dict[i] = words.count(i)
    
    wc = list(dict.items())
    wc.sort(key = lambda x:x[1],reverse=True)
    
    for i in range(10):
        print(wc[i])

  • 相关阅读:
    J.U.C并发框架源码阅读(十五)CopyOnWriteArrayList
    J.U.C并发框架源码阅读(十四)ScheduledThreadPoolExecutor
    J.U.C并发框架源码阅读(十三)ThreadPoolExecutor
    Django基础之request对象
    Django基础之给视图加装饰器
    Django基础之初识视图
    Django基础之CBV和FBV
    Django基础之template
    Django基础之命名空间模式(include)
    Django基础之命名URL和URL反向解析
  • 原文地址:https://www.cnblogs.com/lqy-36/p/7560582.html
Copyright © 2011-2022 走看看