Compare two lists and return match percentage while considering order

Question:

I’m trying to write a program in Python that compares a list of inputs with an existing list then returns the match percentage between the two lists, as if it was verifying answers to an exam.

print ('Please answer the 20 questions with a, b, c, d or en')

correct = ('a','c','d','d','e','b','c','c','a','b','d','c','b','e','c','b','a','e','b','c') #correct answers
answers = [] #empty list that receives the inputs

n = 20
for i in range (0,20):
    ele = input('Insert answer of question %d: ' % (i + 1)) #loop to insert answers into empty list

    answers.append(ele)

overlap = len(set(correct)&set(answers)) / float(len(set(correct)|set(answers))) * 100
print(overlap) #prints percentage obtained

Currently it only checks if the answer is somewhere in the correct list, I need to make it compare each matching index instead.

Asked By: Ian Lohan

||

Answers:

Loop through the two lists in parallel, counting the number of times they match.

correct_count = sum(a == b for a, b in zip(correct, answers))
overlap = correct_count / len(answers) * 100
Answered By: Barmar
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.