Reputation: 227
I know there are other threads with list vs. list, but they don't seem to address my issue. I'm trying to test a series of lists against one specific list and check to see how similar they are. For example,
The "answer key" is as follows:
answerkey = "B,A,D,D,C,B,D,A,C,C,D,B,A,B,A,C,B,D,A,C,A,A,B,D,D"
lanswerkey = list[answerkey]
and I'm trying to test a series of other lists I am importing, that have the format:
N00000023,,A,D,D,C,B,D,A,C,C,,C,,B,A,C,B,D,A,C,A,A
N00000024,C,C,D,D,C,B,,A,C,C,D,B,A,B,,C,B,D,A,C,A,B,B,,D
Could I do something like this?:
right = 0
counter3 = 0
for line in f:
if lanswerkey.split(",")[counter3] == line.split(",")[counter3]:
right += 4
if lanswerkey.split(",")[counter3] != line.split(",")[counter3]:
right -= 1
counter3 += 1
Any help would be appreciated!
Upvotes: 0
Views: 82
Reputation: 5508
In numpy you can easily compare two arrays with symbol ==
, the only problem is you need two arrays with the same length, you can easily deal with this by setting your own rule. In your case, the answerkey
has 25 elements and the line1
has just 22 elements, so you can not compare with them, please set a rule by yourself.
import numpy as np
answerkey = "B,A,D,D,C,B,D,A,C,C,D,B,A,B,A,C,B,D,A,C,A,A,B,D,D"
line1 = "N00000023,,A,D,D,C,B,D,A,C,C,,C,,B,A,C,B,D,A,C,A,A"
line2 = "N00000024,C,C,D,D,C,B,,A,C,C,D,B,A,B,,C,B,D,A,C,A,B,B,,D"
# convert your lists to numpy array
answer = np.array(list(answerkey.split(",")))
q1 = np.array(list(line1.split(","))[1:]) # [1:] just to get rid of the "N00000023"
q2 = np.array(list(line2.split(","))[1:])
# print your array length to check whether they are comparable
print len(answer), len(q1), len(q2)
>> 25 22 25
# get your result array
res = answer == q2
>> [False False True True True True False True
True True True True True True False True True
True True True True False True False True]
# calculate your score
score = sum(res)*4 - (len(res)-sum(res))
print score
>> 70
Upvotes: 2
Reputation:
If you have
answerkey = "B,A,D,D,C,B,D,A,C,C,D,B,A,B,A,C,B,D,A,C,A,A,B,D,D"
answer = "N00000024,B,B,D,D,C,A,D,A,C,D,D,B,A,B,A,C,B,D,A,C,A,A,B,D,D"
# different ^ ^ ^
you can get the number of different positions like this:
len([x for x in zip(answerkey.split(","), answer.split(",")[1:]) if x[0] != x[1]])
# result 3
Upvotes: 1
Reputation: 153
Firstly, get a list of answer from answer key.
>>> answerkey = answerkey.split(",")
['B', 'A', 'D', 'D', 'C', 'B', 'D', 'A', 'C', 'C', 'D', 'B', 'A', 'B',
'A', 'C', 'B', 'D', 'A', 'C', 'A', 'A', 'B', 'D', 'D']
Then read the answer:
>>> answer = raw_input().split(",")[1:]
N00000023,,A,D,D,C,B,D,A,C,C,,C,,B,A,C,B,D,A,C,A,A
['', 'A', 'D', 'D', 'C', 'B', 'D', 'A', 'C', 'C', '', 'C', '', 'B', 'A', 'C', 'B', 'D', 'A', 'C', 'A', 'A']
Lastly, compare the answer:
for i in range(len(answerkey)):
if answerkey[i] == answer[i]:
right += 4
else
right -= 1
If you don't want to use for loop, you may take a look at numpy library which provided the function of element wise comparsion. http://docs.scipy.org/doc/numpy/reference/generated/numpy.equal.html#numpy.equal
Upvotes: 0