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

    1. 实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。
      s='''Twinkle, twinkle, little star,
      How I wonder what you are. 
      Up above the world so high, 
      Like a diamond in the sky. 
      When the blazing sun is gone, 
      When he nothing shines upon, 
      Then you show your little light, 
      Twinkle, twinkle, all the night. 
      Then the traveller in the dark, 
      Thanks you for your tiny spark, 
      He could not see which way to go, 
      If you did not twinkle so. 
      In the dark blue sky you keep, 
      And often through my curtains peep, 
      For you never shut your eye, 
      Till the sun is in the sky. 
      As your bright and tiny spark, 
      Lights the traveller in the dark. 
      Though I know not what you are, 
      Twinkle, twinkle, little star. '''
      s=s.lower()
      s=s.replace(',',' ')
      s=s.replace('.',' ')
      s=s.replace('?',' ')
      s=s.replace('!',' ')
      words=s.split(' ')
      print(words)
      print("little出现的次数为:"+str(words.count("little")))
      print("twinkle出现的次数为:"+str(words.count("twinkle")))

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

      s=list('1211331131323')
      print('列表:',s)
      print('列表长度:',len(s))
      
      s=[int(b) for b in s]
      print('更改为数值型:',s)
      
      s.append(3)
      s.insert(5,2)
      print('增加后列表:',s)
      
      s.pop()
      s.pop(3)
      print('删除后列表:',s)
      
      print('第一个3分的下标:',s.index(3))
      print('1分的同学人数:',s.count(1))
      print('3分的同学人数:',s.count(3))

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

                  答:异:列表是用[ ]表示,元组是用()表示的;  创建了一个列表,你就可以添加,删除,或者是搜索列表中的项目。由于你可以增加或删除项目,我们说列表是可变的数据类型,即这种类型是可以被改变的。而元组通常用在使语句或用户定义的函数能够安全的采用一组值的时候,即被使用的元组的值不会改变。

                        同:列表和元组都是可以嵌套的。

  • 相关阅读:
    java 保留2位小数 转载
    android表格效果ListView隔行变色
    安卓学习之排版RelativeLayout表格布局
    安卓学习之如何关闭所有的activity
    安卓学习之android 数据传递详解(Serialization、Parcelable、Parcel、Intent、Bundle)
    [转]Android 实现TextView中文字链接的方式
    OOP编程iBatis 学习笔记之二 单表增删改操作
    OOP编程iBatis 学习笔记之三 2个表或者多表关联查询
    安卓学习之排版TableLayout表格布局
    (原创)C#反射知识分享之二
  • 原文地址:https://www.cnblogs.com/qisq/p/7574115.html
Copyright © 2011-2022 走看看