DAY8作业

1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话
student = {'name': '张三', 'age': 23, 'score': 80, 'tel': '15382839992'}

2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
all_student = [
{'name': '小明', 'age': 28, 'score': 89, 'tel': '15627382903'},
{'name': '路飞', 'age': 30, 'score': 59, 'tel': '15627382904'},
{'name': '鸣人', 'age': 29, 'score': 60, 'tel': '15627382908'},
{'name': '柯南', 'age': 17, 'score': 45, 'tel': '15627382909'},
{'name': '佐助', 'age': 22, 'score': 90, 'tel': '15627382902'},
{'name': '张楚岚', 'age': 16, 'score': 90, 'tel': '15627382908'}
]

a.统计不及格学生的个数

count = 0
for stu in all_student:
    if stu['score'] < 60:
        count += 1
print('不及格的人数: %d' % count)

b.打印不及格学生的名字和对应的成绩

for stu in all_student:
    if stu['score'] < 60:
        print(stu['name'], stu['score'])

c.统计未成年学生的个数

count = 0
for stu in all_student:
    if stu['age'] < 18:
        count += 1
print('未成年的人数: %d' % count)

d.打印手机尾号是8的学生的名字

for stu in all_student:
    tel = stu['tel']
    if tel[-1] == '8':
        print(stu['name'], tel)

e.打印最高分和对应的学生的名字

max_score = 0
names = []
for stu in all_student:
    score = stu['score']
    if score >= max_score:
        max_score = score

for stu in all_student:
    if stu['score'] == max_score:
        names.append(stu['name'])

print(names, max_score)

f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
all_student = [
{'name': '小明', 'age': 28, 'score': 89, 'tel': '15627382903'},
{'name': '路飞', 'age': 30, 'score': 59, 'tel': '15627382904'},
{'name': '鸣人', 'age': 29, 'score': 60, 'tel': '15627382908'},
{'name': '柯南', 'age': 17, 'score': 45, 'tel': '15627382909'},
{'name': '佐助', 'age': 22, 'score': 90, 'tel': '15627382902'},
{'name': '张楚岚', 'age': 16, 'score': 90, 'tel': '15627382908'}
]

# all_student = [
# {'name': '佐助', 'age': 22, 'score': 90, 'tel': '15627382902'},
# {'name': '张楚岚', 'age': 16, 'score': 90, 'tel': '15627382908'},
# {'name': '小明', 'age': 28, 'score': 89, 'tel': '15627382903'},
# {'name': '鸣人', 'age': 29, 'score': 60, 'tel': '15627382908'},
# {'name': '路飞', 'age': 30, 'score': 59, 'tel': '15627382904'},
# {'name': '柯南', 'age': 17, 'score': 45, 'tel': '15627382909'}
# ]

# nums = [3, 2, 5, 7, 1]

"""
3, 2, 5, 7, 1

i = 0, j = 1,2,3,4
i = 0, j = 1: [3, 2, 5, 7, 1]
j = 2: [5, 2, 3, 7, 1]
j = 3: [7, 2, 3, 5, 1]
j = 4: [7, 2, 3, 5, 1]

i = 1, j = 2,3,4
j = 2: [7, 3, 2, 5, 1]
j = 3: [7, 5, 2, 3, 1]
j = 4: [7, 5, 2, 3, 1]
i = 2, j = 3, 4
j = 3: [7, 5, 3, 2, 1]
j = 4: [7, 5, 3, 2, 1]

i = 3, j = 4
j = 4: [7, 5, 3, 2, 1]

选择排序: 让前面的数依次和后面的每一个数比较大小, 如果后面的比前面的数大就交换位置(降序)。
整个过程执行长度-1次

"""

 选择排序
nums = [3, 2, 5, 7, 1]
length = len(nums)
# i前面的数的下标
for i in range(0, length-1):
    # j表示前面数后边的每个数的下标
    for j in range(i+1, length):
        # 后面的数比前面的大
        if nums[j] > nums[i]:
            # 交换位置
            nums[i], nums[j] = nums[j], nums[i]

print(nums)


length = len(all_student)
for i in range(0, length-1):
    for j in range(i+1, length):
        if all_student[j]['age'] > all_student[i]['age']:
            all_student[i], all_student[j] = all_student[j], all_student[i]
print(all_student)

3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
chinese = {'凛', '律', '鸣', '叶', '陆', '绿', '空', '白'}
math = {'凛', '律', '优', '梓', '澪', '柚', '松', '柏'}
english = {'凛', '鸣', '红', '唯', '澪', '空', '白', '堇'}

a. 求选课学生总共有多少人

all_student = chinese | math | english
print('选课学生的总人数:', len(all_student), all_student)

b. 求只选了第一个学科的人的数量和对应的名字

only_chinese = chinese - math - english
print('只选了语文的学生:', only_chinese)

c. 求只选了一门学科的学生的数量和对应的名字

only_one = (chinese - math - english) | (math - chinese - english) | (english - math - chinese)
print('只选一门学科的学生', only_one)

only_one = (chinese ^ math ^ english) - (chinese & math & english)
print('只选一门学科的学生', only_one)

d. 求只选了两门学科的学生的数量和对应的名字

only_two = ((chinese & math) | (chinese & english) | (english & math)) - (chinese & math & english)
print('只选两门学科的学生', only_two)

e. 求选了三门学生的学生的数量和对应的名字

print(chinese & math & english)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话 2.声明一个列表,在列表中保...
    3981cff33903阅读 143评论 0 0
  • 1 声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话 结果 {'name': 'a...
    生命在于不睡觉阅读 294评论 0 0
  • 1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话 2.声明一个列表,在列表中保...
    蓝色骨头_3f91阅读 221评论 0 1
  • 1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话 2.声明一个列表,在列表中保...
    不语sun阅读 207评论 0 0
  • 周末的中午,我们再次来吃“大拇指”。店里的收银大姐见我们身边跟着小姑娘,大声打起招呼:“哇!你家孩子最近又长高啦~...
    心有林溪1029阅读 227评论 0 1