综合练习:词频统计

时间:2023-02-13 13:08:09
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])

综合练习:词频统计