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])

  • 相关阅读:
    根据第三方提供的wsdl报文(axis2开发),进行的webservice应用的开发实例
    调用axis2开发的接口遇到的问题
    使用 DJ Java Decompiler 将整个jar包反编译成源文件
    解析Myeclipse项目下的.classpath文件
    使用cxf开发webservice应用时抛出异常
    hibernate 映射 多对一
    jquery获取元素的值,获取当前对象的父对象等等
    web项目中加入struts2、spring的支持,并整合两者
    Struts2中的 配置文件
    对list集合中的对象按照对象的某一属性进行排序
  • 原文地址:https://www.cnblogs.com/liulingyuan/p/7560611.html
Copyright © 2011-2022 走看看