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

  1. 实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。
    s="""When I was young I'd listen to the radio
    Waiting for my favorite songs
    When they played I'd sing along,
    It make 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 shalala every wo'wo
    still shines.
    Every shing-a-ling-a-ling that they're starting 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.
    (Shoobie do lang lang)
    Looking bak on how it was in years gone by
    And the good times that had
    makes today seem rather sad,
    So much has changed.
    It was songs of love that I would sing to them
    And I'd memorise each word.
    Those old melodies still sound so good to me
    As they melt the years away
    Every shalala every wo'wo still shines
    Every shing-a-ling-a-ling that they're startingTo sing
    so fine
    All my best memorise come back clearly to me
    Some can even make me cry
    just like before."""
    
    #将所有大写转换为小写
    t=s.lower()
    print('全部转换为小写的结果:'+t+'')
    
    
    #将所有将所有其他做分隔符(,.?!)替换为空格
    for i in ',.?!\'-.':
        p=t.replace(i,' ')
    print('其他分隔符替换为空格的结果:'+p+' ')
    
    
    #统计单词‘every’出现的次数
    count=p.count('every')
    print('单词every出现的次数为:',count)
    print('\n')
    
    #分隔出单词
    y=p.split(' ')
    print('分隔结果为:',y)
    

      

  2. 列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
    mylist=list('12121312321322311332222')
    print(mylist)    
    
    #查询第一个3分的下标
    print('第一个3分的下标:',mylist.index('3'),'\n')
    
    #查询3分的同学有多少个
    print('3分的同学有:',mylist.count('3'),'\n')
    
    #查询1分的同学有多少个
    print('1分的同学有',mylist.count('1'),'\n')    
    
    #加一个3分的同学
    mylist.append('3')
    print('追加后的结果为:',mylist,'\n')
    
    #修改下标为5的同学的分数为3
    mylist[5]='5'
    print('修改后的结果为:',mylist,'\n')
    
    #在下标为6的位置插入一个分数为3的同学
    mylist.insert(6,'3')
    print('插入后的结果为:',mylist,'\n')
    
    #删除最后一个同学的分数
    mylist.pop()
    print('删除后的结果为:',mylist)
    

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

列表:列表通常表示的是相关数据的一个集合。

元组:元组的初始值在后面的程序中将保持不变。

同:都是以表格形式储存数据。

posted on 2017-09-22 19:37  黑木-007  阅读(117)  评论(0)    收藏  举报