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


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

    str='''Just one last dance
    The wine and the lights and the Spanish guitar
    I'll never forget how romantic they are
    But I know' tomorrow I'll lose the one I love
    There's no way to come with you.
    It's the only thing to do
    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
    Oh'Baby
    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'''
    #将所有大写转换为小写#
    str=str.lower()print('全部转换为小写的结果:'+str+'
    ')
    #将所有分隔符(,.?!)替换为空格
    for i in ',.?!':
        str=str.replace(i,' ')print('分隔符替换为空格的结果:'+str+'
    ')
    #统计单词‘just’出现的次数
    count=str.count('just')print('单词just出现的次数为:',count)
    #分隔出一个一个单词
    str=str.split(' ')print('分隔结果为:',str)
     
     

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

    str=list('1223211231212123')
    print('作业评分表:',str)
    
    a=str.index('3')
    print('第一个3分的下标为:',a)
    
    one=str.count('1')
    print('1分的同学有:',one)
    
    three=str.count('3')
    print('3分的同学有:',three)
    
    str.sort()
    print('排序为:',str)

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

    list是处理一组有序项目的数据结构,即可以在一个列表中存储一个序列的项目。列表中的项目。列表中的项目应该包括在方括号中一旦创建了一个列表,可以添加,删除,或者是搜索列表中的项目。列表是可变的数据类型,即这种类型是可以被改变的。

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

    元组,列表可以嵌套。

  • 相关阅读:
    Jvascript方法
    Java高效读取大文件(转)
    RandomAccessFile实时读取大文件(转)
    Java中的逆变与协变 专题
    Docker系列之(一):10分钟玩转Docker(转)
    Redis系列之(一):10分钟玩转Redis(转)
    BDB (Berkeley DB)数据库简单介绍(转载)
    JAVA的extends使用方法
    计算机视觉领域的一些牛人博客,超有实力的研究机构等的站点链接
    中国大推力矢量发动机WS15 跨入 世界先进水平!
  • 原文地址:https://www.cnblogs.com/1244581939cls/p/7577269.html
Copyright © 2011-2022 走看看