Home > OS >  Compare two lists and return match percentage while considering order
Compare two lists and return match percentage while considering order

Time:09-17

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 e\n')

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.

CodePudding user response:

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
  • Related