Reputation: 893
I have a python function that has as an input two variables the so-called current_level and current_affect. The function calculates some rules using training data and those rules can calculating decisions using those two variables the current_level and current_affect. The code contained two lists list_A and list_B. The lists contained the following:
list_A = [["'easy'", "'C'", "'4'", '0.714', '\n'],
["'easy'", "'D'", "'5'", '0.778', '\n'],
["'easy'", "'E'", "'5'", '0.500', '\n'],
["'easy'", "'F'", "'6'", '0.750', '\n']]
list_B = [["'easy'", "'B'", "'2'", '1.000', '\n'],
["'easy'", "'C'", "'3'", '1.000', '\n'],
["'easy'", "'D'", "'4'", '1.000', '\n'],
["'easy'", "'E'", "'5'", '0.875', '\n'],
["'easy'", "'F'", "'6'", '1.000', '\n']]
The first element of the lists correspond to the current_level, the second to the current_affect and the third to the variable score. Therefore what I want is by knowing the input of my method for current_affect to find which of the two lists for this specific case has greater score and return a correspondant value 1 in the case of list_A and -1 for the case of list_B or zero in the equality case. Variable current_affect takes values between A-F. Firstly, how can I add the non-existent current_affect values in my lists. For example list_A is missing levels A and B while list_B is missing level A (I want to add the sublists with zeros) and finally, how can I calculate the return value?
My function is the following:
def association_rules_adaptation(level, current_affect):
Then in the code i have zipped the two lists:
res = zip(list_A,list_B)
Withe the zip I want to link together the two lists regarding the current_affect in order to be able to compare which list has greater score for a specific current_affect. The problem is that when the lists doesn not contained the same exactly values of current_affect I link non-similar things:
(["'easy'", "'C'", "'3'", '0.714', '\n'], ["'easy'", "'B'", "'2'", '1.000', '\n'])
(["'easy'", "'D'", "'4'", '0.778', '\n'], ["'easy'", "'C'", "'3'", '1.000', '\n'])
(["'easy'", "'E'", "'5'", '0.500', '\n'], ["'easy'", "'D'", "'4'", '1.000', '\n'])
(["'easy'", "'F'", "'6'", '0.750', '\n'], ["'easy'", "'E'", "'5'", '0.875', '\n'])
So i want to add all the possible values in my lists in order to be able to link them correctly and then to perform my comparisons.
for row in res:
print (row)
print ("Level", level, ": ", row[0][1])
if (row[0][2] > row[1][2]) or (row[0][2] == row[1][2]):
print ("1")
else:
print ("-1")
EDIT: I want to check which lists are missing and the to add them to lists list_A and list_B, like this:
list_A = [["'easy'", "'A'", "'0'", '0', '\n'], //added sublist
["'easy'", "'B'", "'0'", '0', '\n'], //added sublist
["'easy'", "'C'", "'4'", '0.714', '\n'],
["'easy'", "'D'", "'5'", '0.778', '\n'],
["'easy'", "'E'", "'5'", '0.500', '\n'],
["'easy'", "'F'", "'6'", '0.750', '\n']]
list_B = [["'easy'", "'A'", "'0'", '0', '\n'], //added subilst
["'easy'", "'B'", "'2'", '1.000', '\n'],
["'easy'", "'C'", "'3'", '1.000', '\n'],
["'easy'", "'D'", "'4'", '1.000', '\n'],
["'easy'", "'E'", "'5'", '0.875', '\n'],
["'easy'", "'F'", "'6'", '1.000', '\n']]
Then to zip my lists and perform the comparisons.
Upvotes: 2
Views: 430
Reputation: 1242
list_A = [["'easy'", "'C'", "'4'", '0.714', '\n'],
["'easy'", "'D'", "'5'", '0.778', '\n'],
["'easy'", "'E'", "'5'", '0.500', '\n'],
["'easy'", "'F'", "'6'", '0.750', '\n']]
list_B = [["'easy'", "'B'", "'2'", '1.000', '\n'],
["'easy'", "'C'", "'3'", '1.000', '\n'],
["'easy'", "'D'", "'4'", '1.000', '\n'],
["'easy'", "'E'", "'5'", '0.875', '\n'],
["'easy'", "'F'", "'6'", '1.000', '\n']]
A = zip(*list_A)
a = A[1]
B = zip(*list_B)
b = B[1]
def char_range(c1, c2): //From http://stackoverflow.com/questions/7001144/range-over-character-in-python
"""Generates the characters from `c1` to `c2`, inclusive."""
for c in xrange(ord(c1), ord(c2)+1):
yield chr(c)
i=0
for c in char_range('A','F'):
ch = "'{}'".format(c)
if ch not in a:
list_A.insert(i, ["'easy'",ch,"'0'",'0','\n'])
if ch not in b:
list_B.insert(i, ["'easy'",ch,"'0'",'0','\n'])
i+=1
res = zip(list_A,list_B)
for r in res:
if r[0][2] > r[1][2]:
print 1
elif r[0][2] < r[1][2]:
print -1
else:
print 0
Result :
0
-1
1
1
0
0
Upvotes: 1
Reputation: 727
I would try transforming those lists into a data structure more suited for looking stuff up, such as dictionaries. This is a bit messy, but:
level_lookup = {l[0]: int(l[2]) for l in list_B}
affect_lookup = {l[1]: int(l[2]) for l in list_B}
if current_level in level_lookup:
level_score = level_lookup[current_level]
else:
level_score = 0
if current_affect in affect_lookup:
affect_score = affect_lookup[current_affect]
else:
affect_score = 0
return max(level_score, affect_score)
Upvotes: 1