day6-Dictionary homework

1. Declare a dictionary to store a student’s information, the student information includes: name, age, grades (single subject), phone number, gender (male, female, unknown)

student_message = {
    
    
    'name': 'faker', 
    'age': '30', 
    'score': 99, 
    'tel': '13488668866', 
    'gender': 'man'
}

2. Declare a list and save the information of 6 students in the list (6 dictionaries in question 1)

student_message = [
    {
    
    'name': 'faker',
     'age': 17,
     'score': 88,
     'tel': '13488668866',
     'gender': 'man'},
    {
    
    'name': 'imp',
     'age': 21,
     'score': 59,
     'tel': '13488661234',
     'gender': 'unknown'},
    {
    
    'name': 'uzi',
     'age': 30,
     'score': 58,
     'tel': '13488664321',
     'gender': 'unknown'},
    {
    
    'name': 'IU',
     'age': 27,
     'score': 100,
     'tel': '13488664321',
     'gender': 'woman'},
    {
    
    'name': 'Jay Park',
     'age': 33,
     'score': 100,
     'tel': '12288663458',
     'gender': 'man'},
    {
    
    'name': 'Dok2',
     'age': 35,
     'score': 100,
     'tel': '12288665678',
     'gender': 'man'}

]

​ a. Count the number of failing students

count_fail = 0
for stu in student_message:
    if stu['score'] < 60:
        count_fail += 1
print(count_fail)

​ b. Print the name of the failed student and the corresponding grade

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

​ c. Count the number of underage students

count_nonage = 0
for stu in student_message:
    if stu['age'] < 18:
        count_nonage += 1
print(count_nonage)

​ d. Print the name of the student whose mobile phone end is 8

for stu in student_message:
    if int(stu['tel']) % 10 == 8:
        print(stu['name'])

​ e. Print the highest score and the name of the corresponding student

max_score = 0
for stu in student_message:
    stu_score = stu['score']
    if stu_score > max_score:
        max_score = stu_score
for stu in student_message:
    if stu['score'] == max_score:
        print(stu['name'])

​ f. Sort the list in descending order of student performance

score_reverse = []
new_stu = []
for stu in student_message:
    if stu['score'] not in score_reverse:
        score_reverse.append(stu['score'])
score_reverse.sort(reverse=True)
for x in score_reverse:
    for y in student_message:
        if x == y['score']:
            new_stu.append(y)
print(new_stu)

​ g. Delete all students of unknown gender

for stu in student_message[:]:
    if stu['gender'] == 'unknown':
        student_message.remove(stu)
print(student_message)

3. Use three lists to indicate the names of students who choose courses in three subjects (a student can choose multiple courses at the same time)

subject1 = ['a', 'b', 'c', 'd']
subject2 = ['e', 'f', 'a']
subject3 = ['a', 'd', 'e', 'g', 'h']

​ a. How many students are there in total?

stu = []
for name in subject1:
    if name not in stu:
        stu.append(name)
for name in subject2:
    if name not in stu:
        stu.append(name)
for name in subject3:
    if name not in stu:
        stu.append(name)
print(len(stu))
b. 求只选了第一个学科的人的数量和对应的名字
print('共', len(subject1), '人,名字:', subject1[:])

​ c. Find the number of students who choose only one subject and the corresponding names

count = 0
name_list = []
for name in subject1:
    if name not in subject2 and name not in subject3:
        count += 1
        name_list.append(name)
for name in subject2:
    if name not in subject1 and name not in subject3:
        count += 1
        name_list.append(name)
for name in subject3:
    if name not in subject2 and name not in subject1:
        count += 1
        name_list.append(name)
print('只选了一门学科的学生有', count, '个,名字是:', name_list, sep='')
d. 求只选了两门学科的学生的数量和对应的名字
name_list = []
for name in subject1:
    if name in subject2 and name not in subject3 or name not in subject2 and name in subject3:
        name_list.append(name)
for name in subject2:
    if name in subject1 and name not in subject3 or name not in subject1 and name in subject3:
        name_list.append(name)
for name in subject3:
    if name in subject1 and name not in subject2 or name not in subject1 and name in subject2:
        name_list.append(name)
name_list1 = []
for name in name_list:
    if name not in name_list1:
        name_list1.append(name)
print('只选了两门学科的学生有', len(name_list1), '个,名字是:', name_list1, sep='')

​ e. Find the number of students who have selected three students and their corresponding names

three = []
for name1 in subject1:
    for name2 in subject2:
        for name3 in subject3:
            if name1 == name2 == name3:
                three.append(name1)
print('选了三门学科的学生有', len(three), '个,名字是:', three, sep='')

Guess you like

Origin blog.csdn.net/xdhmanan/article/details/108860120