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


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

    s='''Twinkle, twinkle, little star,
    
    How I wonder what you are.
    
    Up above the world so high,
    
    Like a diamond in the sky.
    
    Twinkle, twinkle, little star,
    
    How I wonder what you are!
    
    When the blazing sun is gone,
    
    When he nothing shines upon,
    
    Then you show your little light,
    
    Twinkle, twinkle, all the night.
    
    Twinkle, twinkle, little star,
    
    How I wonder what you are!
    
    Then the traveler in the dark
    
    Thanks you for your tiny spark;
    
    He could not see which way to go,
    
    If you did not twinkle so.
    
    Twinkle, twinkle, little star,
    
    How I wonder what you are!
    
    Twinkle Twinkle Little Star'''
    s=s.lower()
    print('大写转换为小写:'+s)
    for i in ',.!':
        s=s.replace(i,' ')
    print('所有标点符号替换为空格:'+s)
    print('twinkle出现次数:',s.count("twinkle"))
    print('歌词出现的单词:',s.split(' '),'
    ')
    s='''Twinkle, twinkle, little star,
    
    How I wonder what you are.
    
    Up above the world so high,
    
    Like a diamond in the sky.
    
    Twinkle, twinkle, little star,
    
    How I wonder what you are!
    
    When the blazing sun is gone,
    
    When he nothing shines upon,
    
    Then you show your little light,
    
    Twinkle, twinkle, all the night.
    
    Twinkle, twinkle, little star,
    
    How I wonder what you are!
    
    Then the traveler in the dark
    
    Thanks you for your tiny spark;
    
    He could not see which way to go,
    
    If you did not twinkle so.
    
    Twinkle, twinkle, little star,
    
    How I wonder what you are!
    
    Twinkle Twinkle Little Star'''
    s=s.lower()
    print('大写转换为小写:'+s)
    for i in ',.!':
        s=s.replace(i,' ')
    print('所有标点符号替换为空格:'+s)
    print('twinkle出现次数:',s.count("twinkle"))
    print('分隔单词:',s.split(' '),'
    ')
    大写转换为小写:twinkle, twinkle, little star,
    
    how i wonder what you are.
    
    up above the world so high,
    
    like a diamond in the sky.
    
    twinkle, twinkle, little star,
    
    how i wonder what you are!
    
    when the blazing sun is gone,
    
    when he nothing shines upon,
    
    then you show your little light,
    
    twinkle, twinkle, all the night.
    
    twinkle, twinkle, little star,
    
    how i wonder what you are!
    
    then the traveler in the dark
    
    thanks you for your tiny spark;
    
    he could not see which way to go,
    
    if you did not twinkle so.
    
    twinkle, twinkle, little star,
    
    how i wonder what you are!
    
    twinkle twinkle little star
    所有标点符号替换为空格:twinkle  twinkle  little star 
    
    how i wonder what you are 
    
    up above the world so high 
    
    like a diamond in the sky 
    
    twinkle  twinkle  little star 
    
    how i wonder what you are 
    
    when the blazing sun is gone 
    
    when he nothing shines upon 
    
    then you show your little light 
    
    twinkle  twinkle  all the night 
    
    twinkle  twinkle  little star 
    
    how i wonder what you are 
    
    then the traveler in the dark
    
    thanks you for your tiny spark;
    
    he could not see which way to go 
    
    if you did not twinkle so 
    
    twinkle  twinkle  little star 
    
    how i wonder what you are 
    
    twinkle twinkle little star
    twinkle出现次数: 13
    分隔单词: ['twinkle', '', 'twinkle', '', 'little', 'star', '
    
    how', 'i', 'wonder', 'what', 'you', 'are', '
    
    up', 'above', 'the', 'world', 'so', 'high', '
    
    like', 'a', 'diamond', 'in', 'the', 'sky', '
    
    twinkle', '', 'twinkle', '', 'little', 'star', '
    
    how', 'i', 'wonder', 'what', 'you', 'are', '
    
    when', 'the', 'blazing', 'sun', 'is', 'gone', '
    
    when', 'he', 'nothing', 'shines', 'upon', '
    
    then', 'you', 'show', 'your', 'little', 'light', '
    
    twinkle', '', 'twinkle', '', 'all', 'the', 'night', '
    
    twinkle', '', 'twinkle', '', 'little', 'star', '
    
    how', 'i', 'wonder', 'what', 'you', 'are', '
    
    then', 'the', 'traveler', 'in', 'the', 'dark
    
    thanks', 'you', 'for', 'your', 'tiny', 'spark;
    
    he', 'could', 'not', 'see', 'which', 'way', 'to', 'go', '
    
    if', 'you', 'did', 'not', 'twinkle', 'so', '
    
    twinkle', '', 'twinkle', '', 'little', 'star', '
    
    how', 'i', 'wonder', 'what', 'you', 'are', '
    
    twinkle', 'twinkle', 'little', 'star'] 

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

    fenshu=list("12312122312")
    print('分数:',fenshu)
    print('列表长度:',len(fenshu))
    print('列表最大值:',max(fenshu))
    print('列表最小值:',min(fenshu))
    fenshu.append('2')
    fenshu.insert(1,'3')
    fenshu.pop(2)
    print('输出增插删后列表:',fenshu)
    fenshu=[int(x) for x in fenshu]
    print('更改为数值型:',fenshu)
    print('第一个3分的下标:',fenshu.index(3))
    print('1分的同学人数:',fenshu.count(1))
    print('3分的同学人数:',fenshu.count(3))
    分数: ['1', '2', '3', '1', '2', '1', '2', '2', '3', '1', '2']
    列表长度: 11
    列表最大值: 3
    列表最小值: 1
    输出增插删后列表: ['1', '3', '3', '1', '2', '1', '2', '2', '3', '1', '2', '2']
    更改为数值型: [1, 3, 3, 1, 2, 1, 2, 2, 3, 1, 2, 2]
    第一个3分的下标: 1
    1分的同学人数: 4
    3分的同学人数: 3

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

    异:列表(list)函数新增的元素,会改变列表本身;没有长度限制、元素类型可以不同;可以随时进行增删改操作。

           元组(tuple)一旦初始化,不能进行增删改和排序的操作。

    同:都是有序的序列。

  • 相关阅读:
    UVA-448
    算法提高-集合选取
    算法训练Maze
    UVA-10061
    树状数组
    前缀和
    【UVA
    统计Linux下的CPU状态信息
    Android_内部文件读取
    Android打开/data/目录以及导出文件
  • 原文地址:https://www.cnblogs.com/00js/p/7576519.html
Copyright © 2020-2023  润新知