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

    1. 实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。

    第一题,代码如下:

    day='''Well I wonder could it be
    When I was dreaming about you baby
    You were dreaming of me
    Call me crazy
    Call me blind
    To still be suffering is stupid after all of this time
    Did I lose my love to someone better
    And does she love you like I do
    I do, you know I really really do
    Well hey
    So much I need to say
    Been lonely since the day
    The day you went away
    So sad but true
    For me there's only you
    Been crying since the day
    I remember date and time
    September twenty second
    Sunday twenty five after nine
    In the doorway with your case
    No longer shouting at each other
    There were tears on our faces
    And we were letting go of something special
    Something we'll never have again
    I know, I guess I really really know
    Why do we never know what we've got till it's gone
    How could I carry on
    Cause I've been missing you so much I have to say'''

    day=day.lower()


    for i in ',.?!':
    day=day.replace(i,' ')

    words=day.split(' ')
    print(words)

    print('day :',day.count('day'))
    print('you :',day.count('you'))
    print('went :',day.count('went'))
    print('to :',day.count('to'))
    print('i :',day.count('i'))

    运行结果:

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

    代码如下:

    a=list('123123123')
    print('列表',a)
    for i in range(len(a)):
        a[i]=int(a[i])
    print('数值',a)   
    
    a.append(6)
    
    print('',a)
    
    a.pop()
    
    print('删最后',a)
    
     
    a[2]=1
    
    print('',a)
    
    print('第一个3分的下标为:',a.index(3))
    
    
    print('1分的同学有:',a.count(1))
    
    
    print('3分的同学有:',a.count(3))

    运行结果:

     

     3.简要描述列表与元组的异同

     答:

    列表中的项目应该包括在方括号中,
    你可以添加、删除或是搜索列表中的项目。
    由于你可以增加或删除项目,所以列表是可变的数据类型,
    即这种类型是可以被改变的。元组和列表十分类似,但是元组是不可变的.也就是说你不能修改元组。

    元组通过圆括号中用逗号分割的项目定义。
    元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,
    即被使用的元组的值不会改变。

  • 相关阅读:
    vs2015 停 在 update kb2999226 一直不动
    修复vs2012出现 “无法找到包源”的错误
    forward 和redirect的区别
    软件测试分类
    centos7安装HTTPS协议
    php抓取网页特定div区块及图片,从简单入手
    nginx报 File not found 错误
    关于端口
    调试技巧:让断点停在for循环中的 i 为某个值得时候
    an AC a day keeps the WA away ~
  • 原文地址:https://www.cnblogs.com/decadeyu/p/7562097.html
Copyright © 2011-2022 走看看