用python实现一个
时间: 2023-12-27 09:05:30 浏览: 25
功能:输入一段英文文本,统计其中每个单词出现的次数并输出结果。
实现思路:
1. 输入英文文本
2. 将文本中所有单词以空格为分隔符分开,并转换成小写字母
3. 遍历所有单词,记录每个单词出现的次数
4. 输出结果,按照单词出现次数从大到小排序
代码实现:
```python
text = input("请输入英文文本:")
words = text.lower().split() # 转换成小写字母并以空格分隔单词
word_count = {} # 用字典记录每个单词出现的次数
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
# 按照单词出现次数从大到小排序
sorted_word_count = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
# 输出结果
for word, count in sorted_word_count:
print(f"{word}: {count}")
```
示例输入:
```
请输入英文文本:Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than possible in languages such as C++ or Java.
```
示例输出:
```
python: 2
in: 2
and: 2
code: 2
is: 1
an: 1
interpreted,: 1
high-level,: 1
general-purpose: 1
programming: 1
language.: 1
created: 1
by: 1
guido: 1
van: 1
rossum: 1
first: 1
released: 1
1991,: 1
design: 1
philosophy: 1
emphasizes: 1
readability,: 1
its: 1
syntax: 1
allows: 1
programmers: 1
to: 1
express: 1
concepts: 1
fewer: 1
lines: 1
of: 1
than: 1
possible: 1
languages: 1
such: 1
as: 1
c++: 1
or: 1
java.: 1
```