zoukankan      html  css  js  c++  java
  • 综合练习:词频统计

    song ='''
    There's a girl in my mirror
    I wonder who she is
    Sometimes I think I know her
    Sometimes I really wish I did
    But there's a story in her eyes
    Lullabies and goodbyes
    When she’s lookin’ back at me
    I can tell her heart is broken easily
    ‘Cause the girl in my mirror
    Is cryin’ tonight
    And there's nothing I can tell her
    To make her feel alright
    Oh, the girl in my mirror
    Is cryin' ‘cause of you
    And I wish there was something
    Something I could do
    If I could I would tell her
    Not to be afraid
    The pain that she’s feeling
    Of sense of loneliness will fade
    So dry your tears and rest assured
    Love’ll find your heart deeper
    When she's looking back at me
    I know nothin’ really works that easily
    I can't believe it's what I see
    And that the girl in the mirror
    The girl in the mirror is me...'''
    
    s='''.,"?!;:'''
    for i in s:
        song=song.replace(i,'')
    wordlist=song.lower().split()
    
    
    wordDict={}
    wordSet=set(wordlist)
    for w in wordSet:
        wordDict[w]=wordlist.count(w)
    for i in wordDict:
        print(i,wordDict[i])
    dictList=list(wordDict.items())
    prep={'a','but','in','not','if','will','the','and','so'}
    for i in prep:
        del(wordDict[i])
    wordDict1= sorted(wordDict.items(),key=lambda d:d[1],reverse= True)
    for i in range(10):
        print(wordDict1[i])

  • 相关阅读:
    字符串案例1
    字符串1
    标准类制作
    构造方法
    封装
    成员变量和局部变量
    类和对象的案例

    案例
    方法的参数传递
  • 原文地址:https://www.cnblogs.com/qq974975766/p/8649610.html
Copyright © 2011-2022 走看看