python: compare list that contains multiple lists with a different unique list

6

I am solving a question where I have to do a program that corrects evidence. The program receives a feedback and "n" students responses and compares with the template provided. The answers of all students are put on a single list. My problem is, how to compare the list of answers with the template and print an individual note for each student.

Ex:

gabarito = ['a','b','c','d','e']

resposta_alunos = [['a', 'b', 'c', 'd','d'],['a', 'a', 'c', 'd','d'],...]
    
asked by anonymous 12.09.2016 / 15:31

2 answers

7

I tried to do something as simple as possible. See:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
respostas = [
    ['a', 'b', 'c', 'd', 'e'],
    ['d', 'c', 'c', 'a', 'd'],
    ['d', 'c', 'd', 'b', 'a'],
    ['d', 'c', 'c', 'b', 'e']
];

gabarito = ['d', 'c', 'c', 'b', 'e']

for r_num, resposta in enumerate(respostas):

    total = 0;

    for g_num, valor in enumerate(gabarito):

        # Se a posição do valor do gabarito é o mesmo da resposta e os valores iguais

        if resposta[g_num] == valor:
            #Suponhamos que cada acerto vale 2 pontos
            total = total + 2; 

    print "O aluno %s tirou %d pontos" % (r_num, total)

When running the above code, the result should be:

O aluno 0 tirou 4 pontos
O aluno 1 tirou 6 pontos
O aluno 2 tirou 6 pontos
O aluno 3 tirou 10 pontos

Assuming you have some notion of python , notice that the resposta[g_num] snippet captures through the feedback index the value of one of the responses. This is because, in a template, there is a question number (which would be the index in our case), and therefore should hit both the question number and answer value at the same time.

That's what I got. I do not know if in this case the use of list would be ideal.

    
12.09.2016 / 16:32
1

Another possibility is to use numpy .

Using the response data from @ wallace-maxters, we can get the number of hits as follows.

import numpy as np

respostas = [
    ['a', 'b', 'c', 'd', 'e'],
    ['d', 'c', 'c', 'a', 'd'],
    ['d', 'c', 'd', 'b', 'a'],
    ['d', 'c', 'c', 'b', 'e']
];

gabarito = ['d', 'c', 'c', 'b', 'e']

# Transforma em um array
respostas_arr = np.array(respostas)
gabarito_arr = np.array(gabarito)

# Verifica os acertos
verificacao = respostas_arr == gabarito_arr
#[[False False  True False  True]
# [ True  True  True False False]
# [ True  True False  True False]
# [ True  True  True  True  True]]

# Soma os acertos
acertos = verificacao.sum(1)
#[2 3 3 5]

# Imprime os resultados
for n, i in enumerate(acertos):
    print('O aluno {} acertou {} questões'.format(n+1, i))
#O aluno 1 acertou 2 questões
#O aluno 2 acertou 3 questões
#O aluno 3 acertou 3 questões
#O aluno 4 acertou 5 questões

Compressing everything in one line:

acertos = (np.array(respostas) == np.array(gabarito)).sum(1)
#[2, 3, 3, 5]
    
15.09.2016 / 20:26