python123读取文章并按要求修改

song = “””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!
“””
#去标点符号
s = [‘,’,’.’,’!’]
song1 = “”
for i in song:
    if i in s:
        i = i.replace(i, ” “)
    song1 += i
#print(song1)
#切片
song1 = song1.split()
#print(song1)
#转换大小写
song2 = [i.lower() for i in song1]
#print(song2)
#将单词放入字典中
dict1={}
for i in song2:
    dict1[i] = dict1.get(i,0)+1
print(dict1)

本文为原创文章,转载请注明出处!