英文词频统计预备,组合数据类型练习


  1. 实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。
    s='''When I was yong,
    I’d listen to the radio,
    Waiting for my favorite songs.
    When they played I’d sing along, 
    It made me smile.
    Those were such happy times, 
    And not so long ago. 
    How I wondered where they’d gone.
    But they’re back again, 
    Just like a long lost friend. 
    All the songs I love so well.
    Every sha-la-la-la 
    Enery wo-wo still shines,
    Every shinga-linga-ling,
    That they’restarting to sing so fine.
    When they get to the part !
    Where he,s breaking her heart,
    It can really make me cry,
    Just like before, 
    It’s yesterday once more.
    It was songs of love 
    That I would sing to them, 
    And I’d memorize each word, 
    Those old melodies ,
    Still sound so good to me, 
    As they melt the years away.'''
    s=s.lower() #大写转为小写
    s=s.replace(',',' ') #将,替换为空格
    s=s.replace('.',' ')  #将.替换为空格
    s=s.replace('!',' ')
    print(s)
    words=s.split(' ')
    print(words)
    #统计they出现的次数
    c=s.count('they')
    print('they出现的次数',c)

  2. 列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
    s=list('12032123222')
    print('分数:',s)
    #
    s.append('3')
    print('增加一个3分同学:',s)
    s.insert(0,'0')
    print('在第一个位置前插入一个0分同学:',s)
    #
    s.pop( 3)
    print('删除第三个0分同学:',s)
    s[0]=2
    #
    print('修改第一个同学为2分:',s)
    #查询第一个3分的下标
    a=s.index('3')
    print('第一个3分的下标为:',a)
    #统计1分的人数
    b=s.count('1')
    print('1分的人数为:',b)
    #统计3分的人数
    c=s.count('3')
    print('3分的人数为:',c)
    #变为数值型
    m=[int(i) for i in s]
    print('更改为数值型:',m)
    #排序
    r=m.sort()
    print('排序为:',m)
          

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

答:list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目。

列表中的项目。列表中的项目应该包括在方括号中,这样python就知道你是在指明一个列表。

一旦你创建了一个列表,你就可以添加,删除,或者是搜索列表中的项目。

由于你可以增加或删除项目,我们说列表是可变的数据类型,即这种类型是可以被改变的,并且列表是可以嵌套的。

元祖和列表十分相似,不过元组是不可变的。即你不能修改元组。元组通过圆括号中用逗号分隔的项目定义。

元组通常用在使语句或用户定义的函数能够安全的采用一组值的时候,即被使用的元组的值不会改变。元组可以嵌套。

posted @ 2017-09-22 15:28  爱学习的土豆  阅读(166)  评论(0)    收藏  举报