tags:

views:

81

answers:

1

I have this dictionary

goodDay= {'Class':[1,1,0,0,0,1,0,1,0,1], 'grade':[1,0,0,1,0,1,0,1,0,1]}

I want to traverse the values of first key and also of second and put this condition to check:

If value of K2 is 1 how many times is K1 is 1 and K1 is 0 and if K2 is 0 how many times is K1 is 0 and K1 is 1.

A: 
c = [[0,0],[0,0]]
for first, second in zip(goodDay['class'], goodDay['grade']):
  c[second][first] += 1

You compare the two lists in the dictionary pairwise, since each of the lists has only two values (0 and 1), this means that together(cartesian product) we can have 4 different options (00, 01, 10, 11). So we use 2*2 list to store these. And then iterate through both lists and remember the count into the list. So at the end of execution of above lines, we can read the results from list c as follows:

c[0][0] is the number of zeros in goodDay['class'] where at the same location in goodDay['grade'] is zero
c[0][1] is the number of zeros in goodDay['class'] where at the same location in goodDay['grade'] is one
c[1][0] is the number of ones in goodDay['class'] where at the same location in goodDay['grade'] is zero
c[1][1] is the number of ones in goodDay['class'] where at the same location in goodDay['grade'] is one
Rok
Perhaps you could explain the second half a bit more. (I don't understand that part of the question, either.)
David M.
The Dictionary is as given below:
Compuser7
goodDay= {'Class':[1,1,0,0,0,1,0,1,0,1], 'grade':[1,0,0,1,0,1,0,1,0,1]} I want to code this way that i should get the count of "1" and also "0" in Class when my grade has value "1" and also vice versa i.e. when my grade has value "0"
Compuser7