简体   繁体   中英

Compare two lists and return match percentage while considering order

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM