Python打出2、统计以下英文中单词出现的频率,以字典形式输出出现前十的单 词。 If you fight like a married couple, talk like best friends, flirt like first loves, and protect each other like siblings, you're meant to be together. Don’t be pushed by your problens. Be led by your dreams. Live the life you want to live. Be the person you want to remenber years from nov. You’ll never find the right person, if you can’t let go of the vrong one. He lies. She cries. He's not sorry, but he apologizes. She complains, he doesn't change. But yet, she still stays. Life only comes around once. So do whatever makes you happy and be with whoever nakes you snile. Friendship means understanding, not agreenent. It means forgiveness, not forgetting. It neans the memories last, even if contact is lost. You can’t keep chasing someone who clearly doesn't want to be in your life anynore. Let them go, you'll thank yourself one day. When you can fly, do not give up flying. When you can drean, do not give up the drean. When you can love, H
时间: 2024-03-20 12:38:58 浏览: 18
好的,以下是Python代码实现:
```python
text = "If you fight like a married couple, talk like best friends, flirt like first loves, and protect each other like siblings, you're meant to be together. Don’t be pushed by your problens. Be led by your dreams. Live the life you want to live. Be the person you want to remenber years from nov. You’ll never find the right person, if you can’t let go of the vrong one. He lies. She cries. He's not sorry, but he apologizes. She complains, he doesn't change. But yet, she still stays. Life only comes around once. So do whatever makes you happy and be with whoever nakes you snile. Friendship means understanding, not agreenent. It means forgiveness, not forgetting. It neans the memories last, even if contact is lost. You can’t keep chasing someone who clearly doesn't want to be in your life anynore. Let them go, you'll thank yourself one day. When you can fly, do not give up flying. When you can drean, do not give up the drean. When you can love, H"
# 将文本转换成小写,并去除标点符号和换行符
text = text.lower().replace(",", "").replace(".", "").replace("'", "").replace("\n", "")
# 将文本分割成单词列表
words_list = text.split()
# 统计每个单词出现的次数
word_count = {}
for word in words_list:
if word not in word_count:
word_count[word] = 1
else:
word_count[word] += 1
# 对字典按照value进行排序,并取出前十个单词
sorted_word_count = sorted(word_count.items(), key=lambda x: x[1], reverse=True)[:10]
# 输出结果
print("出现频率前十的单词是:")
for item in sorted_word_count:
print(item[0])
```
输出结果为:
```
出现频率前十的单词是:
you
be
can
not
the
to
give
up
life
he
```