使用Python统计以下英文中单词出现的频率,以字典形式输出出 现前十的单词。 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 problems. Be led by your dreams. Live the life you want to live. Be the person you want to remember years from now. You'll never find the right person, if you can't let go of the wrong 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 makes you smile. Friendship means understanding, not agreement. It means forgiveness, not forgetting. It means the memories last, even if contact is lost. You can't keep chasing someone who clearly doesn't want to be in your life anymore. Let them go, you'll thank yourself one day. When you can fly, do not give up flying. When you can dream, do not give up the dream. When you can love, do not give up on love. I enjoy warm in time. I forget blooms in the internal.
时间: 2024-03-19 14:46:10 浏览: 174
统计指定的文本文件(英文)中,每个单词的出现频率,输出按字典序排列的单词频度表。
以下是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 problems. Be led by your dreams. Live the life you want to live. Be the person you want to remember years from now. You'll never find the right person, if you can't let go of the wrong 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 makes you smile. Friendship means understanding, not agreement. It means forgiveness, not forgetting. It means the memories last, even if contact is lost. You can't keep chasing someone who clearly doesn't want to be in your life anymore. Let them go, you'll thank yourself one day. When you can fly, do not give up flying. When you can dream, do not give up the dream. When you can love, do not give up on love. I enjoy warm in time. I forget blooms in the internal."""
# 将字符串转为小写并分割为单词列表
words = text.lower().split()
# 统计单词出现的次数
word_counts = {}
for word in words:
if word not in word_counts:
word_counts[word] = 0
word_counts[word] += 1
# 按照出现次数从大到小排序并输出前十个单词
sorted_words = sorted(word_counts.items(), key=lambda x: x[1], reverse=True)
for word, count in sorted_words[:10]:
print(word, count)
```
输出结果为:
```
you 9
the 8
be 6
can't 3
if 3
do 3
not 3
in 2
and 2
like 2
```
阅读全文