
1.分析题目,思考问题与解答:
【1】.输入?以空格分隔?
我最先想到的是以下版本:
s = list(input().split(' '))
这个会出现答案错误
原因:用了 split() 处理后就已经是列表形式了,再用 list 就是多此一举。默认split()会忽略多个空格,split(' ')可能产生多个空格
【2】.怎么统计各行就业学生数量?
这个我不知道
参考豆包后:
创建一个空字典,因为输出形式像字典。count_dict = {}遍历 for industry in s ,然后统计数量:

解释:


【3】.数量怎么按从高到低输出

详解:
sorted :已排序的,item : 项目

lambda:无明函数






【5】怎么输出?
| 循环次数 | item 的值 | item [0] (行业名) | item [1] (数量) | 打印结果 |
|---|---|---|---|---|
| 1 | ('计算机', 3) |
计算机 | 3 | 计算机:3 |
| 2 | ('交通', 2) |
交通 | 2 | 交通:2 |
| 3 | ('金融', 1) |
金融 | 1 | 金融:1 |
OK齐活!来看整体效果:
python
s = input().split()
count_dict = {}
for industry in s:
if industry in count_dict:
count_dict[industry] += 1
else:
count_dict[industry] = 1
sorted_items = sorted(count_dict.items(),key = lambda x:x[1],reverse = True)
for item in sorted_items:
print(f"{item[0]}:{item[1]}")