组合数据类型练习,英文词频统计实例9-21

  • 1、列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
    >>>score=list('21223113321')
    >>>print('作业评分列表:',score)
    >>>score.append('3')
    >>>print('增加:',score)
    >>>score.pop()
    >>>print('删除:',score)
    >>>score.insert(2,'1')
    >>>print('插入:',score)
    >>>score[2]='2'
    >>>print('修改:',score)
    >>>print('第一个3分的下标:',score.index('3'))
    >>>print('1分的个数:',score.count('1'))
    >>>print('3分的个数:',score.count('3'))
    
    
  • 2、字典实例:建立学生学号成绩字典,做增删改查遍历操作。
    >>>d={'洪英杰':93,'郭子维':74,'王五':45,'徐均钧':66}
    >>>print('学生成绩字典:',d)
    >>>d['钱二']=92
    >>>print('增加:',d)
    >>>d.pop('徐均钧')
    >>>print('删除:',d)
    >>>d['张三']=73
    >>>print('修改:',d)
    >>>print('查询李四成绩:',d.get('李四',''))
    
    
  • 3、列表,元组,字典,集合的遍历。
    >>>ls=list("4613125646")
    >>>tu=tuple("4613125646")
    >>>s=set("4613125646")
    >>>d={'洪英杰':93,'郭子维':74,'王五':45,'徐均钧':66}
    >>>print("列表:",ls)
    >>>for i in ls:
        print(i,end=' ')
    >>>print("\n")
    >>>print("元组:",tu)
    >>>for i in tu:
        print(i,end=' ')
    >>>print("\n")
    >>>print("集合:",s)
    >>>for i in s:
        print(i,end=' ')
    >>>print("\n")
    >>>print("字典:",d)
    >>>for i in d:
        print(i,end='\t')
    >>>print(d[i],end='\n')

  • 4、英文词频统计实例
    1. 待分析字符串
    2. 分解提取单词
      1. 大小写 txt.lower()
      2. 分隔符'.,:;?!-_’
    3. 计数字典
    4. 排序list.sort()
    5. 输出TOP(10)

      • faded = '''You were the shadow to my light
        Did you feel us?
        Another start
        You fade away
        Afraid our aim is out of sight
        Wanna see us
        Alive
        Where are you now?
        Where are you now?
        Where are you now?
        Was it all in my fantasy?
        Where are you now?
        Were you only imaginary?
        Where are you now?
        Atlantis
        Under the sea
        Under the sea
        Where are you now?
        Another dream
        The monster's running wild inside of me
        I'm faded
        I'm faded
        So lost, I'm faded
        I'm faded
        So lost, I'm faded
        These shallow waters never met what I needed
        I'm letting go a deeper dive
        Eternal silence of the sea. I'm breathing alive
        Where are you now?
        Where are you now?
        Under the bright but faded lights
        You've set my heart on fire
        Where are you now?
        Where are you now?
        Where are you now?
        Atlantis
        Under the sea
        Under the sea
        Where are you now?
        Another dream
        The monster's running wild inside of me
        I'm faded
        I'm faded
        So lost, I'm faded
        I'm faded
        So lost, I'm faded'''
        
        faded = faded.lower()
        for i in '?!,.\'\n':
            faded = faded.replace(i,' ')
        words = faded.split(' ')
        
        dic={}
        keys = set(words)
        for i in keys:
            dic[i]=words.count(i)
        
        c = list(dic.items())
        c.sort(key=lambda x:x[1],reverse=True)
        
        for i in range(10):
            print(c[i])
        
        
      • 建立学生学号成绩字典

        >>>score = {10:'41',
                     20:'64',
                     30:'71',
                     40:'81',
                     50:'91'
                    }
        >>>print(score)
        >>>score[6] = '90'
        >>>print(score)
        >>>score.pop(6)
        >>>print(score)
         
        >>>for i in score:
            print("{:>2} : {:<2}".format(i,score.get(i)))
        
        

        列表,元组,字典,集合

        >>>ls=list("4613125646")
        >>>tu=tuple("4613125646")
        >>>se=set("4613125646")
        >>>d={'阿一':93,'阿二':74,'阿三':45,'阿四':66}
        >>>print("列表:",ls)
        >>>for i in ls:
            print(i,end=' ')
        >>>print("\n")
        >>>print("元组:",tu)
        >>>for i in tu:
            print(i,end=' ')
        >>>print("\n")
        >>>print("集合:",se)
        >>>for i in se:
            print(i,end=' ')
        >>>print("\n")
        >>>print("字典:",d)
        >>>for i in d:
            print(i,end='\t')
            print(d[i],end='\n')
        
        

        歌词统计

        >>>song = '''Vincent,Don McLean. Starry starry night
         paint your palette blue and grey
         look out on a summer\'s day
         with eyes that know the darkness in my soul.
         Shadows on the hills
         sketch the trees and the daffodils
         catch the breeze and the winter chills
         in colors on the snowy linen land.
         And now I understand
         what you tried to say to me
         and how you suffered for your sanity
         and how you tried to set them free.
         They would not listen they did not know how
         perhaps they\'ll listen now.
         Starry starry night
         flaming flowers that brightly blaze
         swirling clouds in violet haze
         reflect in Vincent\'s eyes of China blue.
         Colors changing hue
         morning fields of amber grain
         weathered faces lined in pain
         are smoothed beneath the artist\'s loving hand.
         And now I understand
         what you tried to say to me
         and how you suffered for your sanity
         and how you tried to set them free.
         They would not listen they did not know how
         perhaps they\'ll listen now.
         For they could not love you
         but still your love was true
         and when no hope was left in sight on that
         starry starry night.
         You took your life as lovers often do,
         But I could have told you Vincent
         this world was never meant for one as beautiful as you.
         Starry starry night
         portraits hung in empty halls
         frameless heads on nameless walls
         with eyes that watch the world and can\'t forget.
         Like the stranger that you\'ve met
         the ragged men in ragged clothes
         the silver thorn of bloddy rose
         lie crushed and broken on the virgin snow.
         And now I think I know
         what you tried to say to me
         and how you suffered for your sanity
         and how you tried to set them free.
         They would not listen they\'re not listening still
         perhaps they never will.'''
         
        >>>song = song.lower()
        >>>for i in '?!,.\'\n':
        >>> song = song.replace(i,' ')
        >>>words = song.split(' ')
         
        >>>dic={}
        >>>keys = set(words)
        >>>for i in keys:
        >>>dic[i]=words.count(i)
         
        >>>c = list(dic.items())
        >>>c.sort(key=lambda x:x[1],reverse=True)
         
        >>>for i in range(10):
            >>>print(c[i])
        
        

         

posted @ 2017-09-21 14:58  洪英杰  阅读(146)  评论(0编辑  收藏  举报