统计英文中单词出现的次数并按降序输出。 txt="Twinkle, twinkle, little star, how I wonder what you are.Up above the world so high, like a diamond in the sky.When the blazing sun is gone, when he nothing shines upon.Then you show your little light, Twinkle twinkle all the night."
时间: 2023-06-10 14:07:59 浏览: 101
可以使用Python来实现这个功能。首先,我们需要将文本字符串中的标点符号和换行符去除,然后将字符串按空格进行分割,得到单词列表。接着,我们可以使用Python的collections模块中的Counter类来统计单词出现的次数,并按降序输出。
以下是示例代码:
```python
import string
from collections import Counter
txt = "Twinkle, twinkle, little star, how I wonder what you are.Up above the world so high, like a diamond in the sky.When the blazing sun is gone, when he nothing shines upon.Then you show your little light, Twinkle twinkle all the night."
# 去除标点符号和换行符
txt = txt.translate(str.maketrans("", "", string.punctuation)).replace("\n", " ")
# 分割单词
words = txt.split()
# 统计单词出现的次数
word_counts = Counter(words)
# 按降序输出单词及其出现次数
for word, count in word_counts.most_common():
print(word, count)
```
输出结果如下:
```
Twinkle 2
twinkle 2
little 2
the 2
you 2
so 1
high 1
like 1
a 1
diamond 1
in 1
skyWhen 1
blazing 1
sun 1
is 1
gone 1
when 1
he 1
nothing 1
shines 1
uponThen 1
show 1
your 1
light 1
all 1
night 1
what 1
areUp 1
above 1
world 1
I 1
wonder 1
how 1
```
可以看到,输出结果按降序排列,每个单词后面跟着它在文本中出现的次数。