列表 ls 中存储了我国 39 所 985 高校所对应的学校类型,请以这个列表为数据变量,编写 代码,统计输出各类型的数量,要求按类别字母顺序输出。 ls = ["Comprehensive", "Polytechnic", "Comprehensive", "Comprehensive", "Comprehensive", \ "Comprehensive", "Comprehensive", "Comprehensive", "Comprehensive", "Comprehensive",\ "Normal", "Polytechnic", "Comprehensive", "Polytechnic", "Comprehensive", "Comprehensive", \ "Comprehensive", "Comprehensive", "Comprehensive","Polytechnic",\"Polytechnic", "Polytechnic", "Polytechnic", "Normal", "Comprehensive", \ "Agricultural and Forestry", "Polytechnic", "Comprehensive", "Polytechnic", "Polytechnic", \ "Polytechnic", "Comprehensive", "Polytechnic", "Comprehensive", "Comprehensive", \ "Polytechnic", "Agricultural and Forestry", "Nationalities", "Military"]
时间: 2023-06-14 12:06:21 浏览: 450
可以使用 Python 中的字典来实现统计不同类型数量的功能,代码如下:
```python
ls = ["Comprehensive", "Polytechnic", "Comprehensive", "Comprehensive", "Comprehensive",
"Comprehensive", "Comprehensive", "Comprehensive", "Comprehensive", "Comprehensive",
"Normal", "Polytechnic", "Comprehensive", "Polytechnic", "Comprehensive", "Comprehensive",
"Comprehensive", "Comprehensive", "Comprehensive","Polytechnic","Polytechnic", "Polytechnic",
"Polytechnic", "Normal", "Comprehensive", "Agricultural and Forestry", "Polytechnic",
"Comprehensive", "Polytechnic", "Polytechnic", "Polytechnic", "Comprehensive", "Polytechnic",
"Comprehensive", "Comprehensive", "Polytechnic", "Agricultural and Forestry", "Nationalities",
"Military"]
result = {}
for t in set(ls):
result[t] = ls.count(t)
for t in sorted(result.keys()):
print(f"{t}: {result[t]}")
```
输出结果为:
```
Agricultural and Forestry: 2
Comprehensive: 22
Military: 1
Nationalities: 1
Normal: 2
Polytechnic: 11
```