How do I compare dictionary meanings (lists) to a single list?
Let's say I was given a dictionary:
students = {"adrian":["a","b","c","d"], "jamie":["b","b","a","d"], "adam":["a","c","d","d"]}
And the list:
answers=["a","b","c","d"]
I just want to check that each item matches the responses from its pointer to the values ββin the dictionary. Briefly compare each list with the answers. I would then print the number of times the student was correct.
For example, if I were to compare the value of the key "adrian" with the responses I would get 4. And if I were to compare jamie to the responses, I would get 2. And if I were to compare adam to the responses, I would get 2.
How can I compare the two?
Thanks a lot for any help!
>>> students = {"adrian":["a","b","c","d"], "jamie":["b","b","a","d"], "adam":["a","c","d","d"]}
>>> answers = ["a","b","c","d"]
>>> {s:sum(t == a for t, a in zip(students[s], answers)) for s in students}
{'jamie': 2, 'adam': 2, 'adrian': 4}
This is for each student the zip
answers to their answers with the answers and then compares them and then sums up the resulting booleans.
import pandas as pd
students = {"adrian":["a","b","c","d"], "jamie":["b","b","a","d"], "adam":["a","c","d","d"]}
answers=["a","b","c","d"]
df = pd.DataFrame(students)
df.apply(lambda x: x==answers).sum()
Explanation of the bits:
df.apply(lambda x: x==answers)
will compare the answers for each student, resulting in the following array:
adam adrian jamie
0 True True False
1 False True True
2 False True False
3 True True True
.sum () will do the column sum (cast True to 1 and False to 0), decreasing the array to:
adam 2
adrian 4
jamie 2
something like that?
def get_correct_answers_count(given_answers,
correct_answers):
return sum(1
for student_answer, correct_answer in zip(given_answers,
correct_answers)
if student_answer == correct_answer)
students_correct_answers_count = {
student_name: get_correct_answers_count(given_answers=student_answers,
correct_answers=answers)
for student_name, student_answers in students.items()}
gives us
{'adrian': 4, 'jamie': 2, 'adam': 2}
You can use zip () to get a combination of values ββand responses
for k,v in students.items():
ans = 0
for i, j in zip(v,answers):
if i==j:
ans+=1
print ans
I think it works
students = {"adrian":["a","b","c","d"],
"jamie":["b","b","a","d"],
"adam":["a","c","d","d"]}
answers = ["a","b","c","d"]
results = {}
for k,v in students.items():
results [k] = 0
for i,answer in enumerate(v):
if answers[i] == v[i]:
results[k]+=1
print (results)
{'adrian': 4, 'adam': 2, 'jamie': 2}
Set () solution
Example
data = {'jamie': ['a', 'b', 'c', 'd']}
answers = ['b', 'd', 'c', 'd']
tuples = map(set, zip(data['jamie'], answers))
reduce(lambda y, coll: (len(coll) - 1) + y , tuples, 0)
Out[8]: 2
The example exploits the fact that a set of data types can only contain unique data. So by picking two lists of jamie and the responses, I get tuples. If a tuple contains two of them, it set()
will make it unique.
When I shorten the list of tuples, I am taking advantage of the fact that if the set contains more than one element, it is a sign of a wrong answer and counting it.