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

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

    >>> ls=list('1231323232323131323')
    >>> ls
    ['1', '2', '3', '1', '3', '2', '3', '2', '3', '2', '3', '2', '3', '1', '3', '1', '3', '2', '3']
    >>> ls.append('4')
    >>> ls.append('5')
    >>> ls
    ['1', '2', '3', '1', '3', '2', '3', '2', '3', '2', '3', '2', '3', '1', '3', '1', '3', '2', '3', '4', '5']
    >>> ls.pop()
    '5'
    >>> ls
    ['1', '2', '3', '1', '3', '2', '3', '2', '3', '2', '3', '2', '3', '1', '3', '1', '3', '2', '3', '4']
    
    >>> ls.index('3')
    2
    >>> ls.count('1')
    4
    >>> ls.count('3')
    9
    >>> 

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

    >>> d={'01':'95','02':'85','03':'90','04':'92','05':'72','06':'93'}
    >>> d.keys()
    dict_keys(['01', '02', '03', '04', '05', '06'])
    >>> d.values()
    dict_values(['95', '85', '90', '92', '72', '93'])
    >>> d.get('03','100')
    '90'
    >>> d['99']='99'
    >>> d
    {'01': '95', '02': '85', '03': '90', '04': '92', '05': '72', '06': '93', '99': '99'}

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

    >>> l=list('123456665432145')
    >>> t=tuple('65432566123333')
    >>> d={'01':'','02':'','03':'','04':''}
    >>> s=set('123654598777296582')
    >>> l
    ['1', '2', '3', '4', '5', '6', '6', '6', '5', '4', '3', '2', '1', '4', '5']
    >>> 
    >>> t
    ('6', '5', '4', '3', '2', '5', '6', '6', '1', '2', '3', '3', '3', '3')
    >>> 
    >>> d
    {'01': '', '02': '', '03': '', '04': ''}
    >>> 
    >>> s
    {'8', '2', '7', '9', '1', '3', '6', '5', '4'}
    >>> 

    列表使用方括号,元组使用小括号,字典使用花括号,集合使用[()];

    元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可,列表则复杂多;

    列表、字典、集合能增加、修改、删除,元组不能;

    4.英文词频统计实例

    new = '''An empty street An empty house 
    A hole inside heart  all alone and the rooms 
    Are getting smaller 
    I wonder how I wonder why 
    I wonder where they are 
    The days we had 
    The songs we sang together 
    And oh! my love  holding on forever 
    Reaching for a love 
    That seems so far 
    So I say a litter prayer 
    No my dream will take me there 
    Where the skies are blue to see you 
    once again my love 
    Overseas from coast to coast 
    Find a place I love the most 
    Where the fields are green 
    To see you once again 
    My love 
    I Try to read I go to work  laughing with my friends 
    But I can't stop to keep myself 
    From thinking 
    I wonder how I wonder why 
    I wonder where they are 
    The days we had 
    The songs we sang togetther 
    And oh! my love 
    I'mholding on forever 
    Reaching for a love 
    That seems so far 
    So I say a litter prayer 
    No my dream will take me there 
    Where the skies are blue to see you 
    once again my love 
    Overseas from coast to coast 
    Find a place I love the most 
    Where the fields are green 
    To see you once again 
    To hold you in my arms 
    To promise my love 
    To tell you from my heart 
    You are all I'm thinking of 
    
    Reaching for a love 
    That seems so far 
    So I say a litter prayer 
    No my dream will take me there 
    Where the skies are blue to see you 
    once again my love 
    Overseas from coast to coast 
    Find a place I love the most 
    Where the fields are green 
    To see you once again 
    My love 
    And hope my dream will take me there 
    Where the skies are blue to see you 
    once again my love 
    Overseas from coast to coast 
    Find a place I love the most 
    Where the fields are green 
    To see you once again 
    My love'''
    
    new=new.lower() 
    for i in '.,"':
        new=new.replace(i,' ')
    word = new.split(' ') 
    dic ={}
    keys = set(word)
    for i in keys:
        dic[i]=word.count(i)
    words = list(dic.items())
    words.sort(key= lambda x:x[1],reverse = True)
    for i in range(10):
        print(word[i])

  • 相关阅读:
    【2020春】李宏毅机器学习(Classification/高斯分布/逻辑回归vs线性回归)
    【2020春】李宏毅机器学习(New Optimizers for Deep Learning)
    【2020春】李宏毅机器学习(Gradient Descent)
    SinGAN: Learning a Generative Model from a Single Natural Image
    HoloGAN: Unsupervised Learning of 3D Representations From Natural Images
    styleGAN相关
    styleGAN相关
    styleGAN相关论文
    styleGAN相关论文
    styleGAN相关论文
  • 原文地址:https://www.cnblogs.com/liulingyuan/p/7560611.html
Copyright © 2011-2022 走看看