键盘输入某班同学就业的行业名称,行业之间用空格间隔。统计各行业就业的学生数量,按照数量从高到低输出。用Python解决上述问题
时间: 2024-01-21 18:17:51 浏览: 30
可以使用Python中的字典来实现这个功能,具体代码如下:
```python
industry_dict = {}
industries = input("请输入同学就业的行业名称(用空格隔开):").split()
# 统计各行业就业的学生数量
for industry in industries:
if industry in industry_dict:
industry_dict[industry] += 1
else:
industry_dict[industry] = 1
# 按照数量从高到低输出
sorted_industry_dict = sorted(industry_dict.items(), key=lambda x: x[1], reverse=True)
for industry, count in sorted_industry_dict:
print("{}: {}人".format(industry, count))
```
运行示例:
```
请输入同学就业的行业名称(用空格隔开):IT IT 金融 金融 金融 医疗 IT
金融: 3人
IT: 2人
医疗: 1人
```
相关问题
用Java容器编写 从键盘输入某各个同学行业名称,杭特之间用空格间隔,统计各行业就业的学生数量,按数量从高到低输出
可以使用Java中的HashMap来实现统计各行业就业学生数量的功能。具体代码如下:
```java
import java.util.*;
public class IndustryStatistics {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入同学们的行业名称(用空格间隔):");
String input = scanner.nextLine();
String[] industries = input.split(" ");
Map<String, Integer> industryCountMap = new HashMap<>();
for (String industry : industries) {
if (industryCountMap.containsKey(industry)) {
int count = industryCountMap.get(industry);
industryCountMap.put(industry, count + 1);
} else {
industryCountMap.put(industry, 1);
}
}
List<Map.Entry<String, Integer>> sortedList = new ArrayList<>(industryCountMap.entrySet());
sortedList.sort(Map.Entry.comparingByValue(Comparator.reverseOrder()));
System.out.println("按数量从高到低输出各行业就业学生数量:");
for (Map.Entry<String, Integer> entry : sortedList) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
```
这段代码首先通过`Scanner`从键盘读取同学们的行业名称,然后使用`split`方法将输入的字符串拆分成行业数组。接下来,使用HashMap统计每个行业出现的次数,如果HashMap中已存在该行业,则计数加1;如果不存在,则将该行业放入HashMap并初始化计数为1。
然后,将HashMap中的键值对转化为List,并使用`sort`方法根据值(学生数量)进行降序排序。最后,遍历排序后的List,按照格式输出各行业就业学生数量。
你可以尝试运行这段代码,输入同学们的行业名称,即可得到按数量从高到低输出的结果。
用python键盘输入某班各个同学就业的行业名称,行业名称之间用空格间隔(回车结束输入) 。统计各行业就业的学生数量,按数量从高到低方式输出。
以下是用 Python 实现的代码:
```python
from collections import Counter
jobs = []
while True:
job = input("请输入学生就业的行业名称(回车结束输入):")
if not job:
break
jobs.extend(job.split())
counter = Counter(jobs)
for job, count in counter.most_common():
print(f"{job}: {count}")
```
这个程序会不断地让用户输入学生就业的行业名称,直到用户输入一个空行为止。然后,它会统计每个行业出现的次数,并按照出现次数从高到低的顺序输出结果。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)