建立一个字典数据,每个条目为:{"学生姓名":["性别","地方","爱好","班级"]…} 输入你班级10个同学的信息,分类统计并输出!
时间: 2023-11-11 18:03:14 浏览: 77
可以建立一个字典数据,每个条目为:{"学生姓名":["性别","地方","爱好","班级"]},然后输入班级10个同学的信息,分类统计并输出。
```python
# 定义字典数据
students = {}
# 输入10个同学的信息
for i in range(10):
name = input("请输入第{}个同学的姓名:".format(i+1))
gender = input("请输入第{}个同学的性别:".format(i+1))
place = input("请输入第{}个同学的地方:".format(i+1))
hobby = input("请输入第{}个同学的爱好:".format(i+1))
class_name = input("请输入第{}个同学的班级:".format(i+1))
students[name] = [gender, place, hobby, class_name]
# 分类统计并输出
gender_count = {"男": 0, "女": 0}
place_count = {}
hobby_count = {}
class_count = {}
for student in students.values():
gender_count[student[0]] += 1
if student[1] in place_count:
place_count[student[1]] += 1
else:
place_count[student[1]] = 1
if student[2] in hobby_count:
hobby_count[student[2]] += 1
else:
hobby_count[student[2]] = 1
if student[3] in class_count:
class_count[student[3]] += 1
else:
class_count[student[3]] = 1
print("性别统计:", gender_count)
print("地方统计:", place_count)
print("爱好统计:", hobby_count)
print("班级统计:", class_count)
```
阅读全文