Home > other >  I need to compare two lists, index by index, and count the matches
I need to compare two lists, index by index, and count the matches

Time:07-14

First of all, I'm in my second week of learning python, so I'm a complete newbie, and am learning for fun and hobby, and by my own.

I'm making a small program to evaluate N multiple choices answers given by students.

It will generate 2 lists from user inputs, the first list will consist of the students answers, and the second, the correct answers.

So, first, I take an integer input telling the program how many questions will be evaluated, then it prompts the user to give the student answers, followed by the correct answers.

I need the program to evaluate if the answers are correct. As of now, I can only make the program compare the lists without regarding the index position, which is adamant in the program, since question 1 is different from question 2, and the answer will change.

This is what I've worked out so far in order to take the inputs correctly:

quest = int(input())
score = 0
stud = []
prof = []

for i in range(quest):
    ans = input()
    stud.append(ans)

for i in range(quest):
    gab = input()
    prof.append(gab)

So, no I have the list stud, containing the student answers, and the list prof, containing the correct answers, and need to compare both, index by index, to analise weather that particular index matches in both lists, and return me the number of times it matches.

I've thought about creating a third list with the indexes of the matches, and resolving the score with an len() function in this third list, but I can't think of how to compare the lists.

CodePudding user response:

You can use sum and zip in a generator expression:

sum(1 for i, j in zip(stud, prof) if i == j)

CodePudding user response:

Couldn't you do something like this?

for i in range(quest):
if stud[i] == prof[i]:
    score  = 1

CodePudding user response:

It's not necessary to create a list with indexes since you already have them in range(quest). You have used them already.

But you could generate a new list with the comparisons:

results = []
for i in range(quest):
    results.append(stud[i] == prof[i])

or, more elegantly, you can use list comprehension:

results = [stud[i] == prof[i] for i in range(quest)]

This is going to generate a list of Booleans, e.g. [True, True, False, False], and then you can add them. Python will convert True to 1 and False to 0, this is called type conversion.

grade = sum(results)

The advantage is that you get the list with the result for each question if, for example, you want later to tell the student in which questions he failed.

CodePudding user response:

Note that the answer proposed by Hugh works and is more efficient than this one.

If you also want a list that indicates which answers are correct and which are not, you can also do this:

stud = ['a', 'd', 'c', 'c', 'b']
prof = ['a', 'e', 'c', 'e', 'b']
nb_of_questions = len(stud)
correct = [0]*nb_of_questions

for i in range(nb_of_questions):
    if stud[i] == prof[i]:
        correct[i] = 1
grade = sum(correct)

print('correct answers:', correct)
print('grade:', grade)

Which returns:

correct answers: [1, 0, 1, 0, 1]
grade: 3
  • Related