Home > Net >  How can I eliminate matched names from below Code?
How can I eliminate matched names from below Code?

Time:08-10

I am unable to understand how can I eliminate the record Student Created inside Else 1st name from the result. As it was matched it should have been exited from the loop. Then why is it being printed in output?

#Code is ready to use

Code:

student_for_admission = [{"student_name": "1st name", "other_key": "other_val"},{"student_name": "johndoe", "student":"other_val"}]
Student_eligible = [{"name": "1st name", "other_key": "other_val1", "another_key": "another_val2"},{"name": "2nd name", "other_key": "other_val", "another_key": "another_val"},{"name": "3rd name", "other_key": "other_val", "another_key": "another_val"}]
student_created=0
student_updated=0
student_skipped=0
for student in student_for_admission:
    for eligible in Student_eligible:
        if eligible["name"] == student["student_name"]:
            # Matching student found
            print("Student updated "   eligible["name"])
            print("Skipped "   eligible["name"])
            student_skipped  = 1
            print(student_skipped)
            print("Before break")
            break
            print("After break")
        else:
            # Create Student
            print("Student Created inside Else "  eligible["name"])
            student_created  = 1

Output:

Student updated 1st name
Skipped 1st name
1
Before break
Student Created inside Else 1st name
Student Created inside Else 2nd name
Student Created inside Else 3rd name

CodePudding user response:

enter image description here

When it's checking for johndoe -> each and everytime if condition got false - that's why 1st name got printed

CodePudding user response:

You're right that the code exited from the loop, and it is the inner for loop. The code then moved onto the next student in student_for_admission.

The matched name is being printed in the output because you are printing the name from Student_eligible instead of from student, so it is printing out the ones that are eligible hence "matched name".

Try:

print("Student Created inside Else "  student["student_name"])

in your else statement.

CodePudding user response:

You can try this solution, You need to add addition conditions as I mentioned in the below code.

student_for_admission = [{"student_name": "1st name", "other_key": "other_val"},
                         {"student_name": "johndoe", "student":"other_val"}]
Student_eligible = [{"name": "1st name", "other_key": "other_val1", "another_key": "another_val2"},
                    {"name": "2nd name", "other_key": "other_val", "another_key": "another_val"},
                    {"name": "3rd name", "other_key": "other_val", "another_key": "another_val"}]
student_created = 0
student_updated = 0
student_skipped = 0
students_visited = []
for student in student_for_admission:
    for eligible in Student_eligible:
        if eligible["name"] == student["student_name"]:
            # Matching student found
            print("Student updated "   eligible["name"])
            print("Skipped "   eligible["name"])
            student_skipped  = 1
            print(student_skipped)
            print("Before break")
            students_visited.append(eligible['name'])
            break
        elif eligible['name'] not in students_visited:
            # Create Student
            print("Student Created inside Else "   eligible["name"])
            student_created  = 1

I'm skipping those students which are visited/considered already.

  • Related