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

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

    元组,列表可以嵌套。

  • 相关阅读:
    node起本地服务器以及实现代理,前端接口转发
    一键前端代理,一行命令开启nginx容器,代理前端页面
    go语言学习笔记
    patch需要数据格式前端算法,patch算法基础,两个对象对比取差异属性
    react-native中使用Echarts,自己使用WebView封装Echarts经验
    如何用js自己实现Animate运动函数
    vue中的表单异步校验方法封装
    Entity Framework6使用SQL Server Compact免安装部署
    WCF异常传播
    解决.net的堆碎片化带来的内存占用过大的问题
  • 原文地址:https://www.cnblogs.com/1244581939cls/p/7577269.html
Copyright © 2011-2022 走看看