2

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.

4
  • 3
    please edit the question with your code. Commented Apr 14, 2017 at 13:07
  • The expected output is 1,-1, and 0. Commented Apr 14, 2017 at 13:08
  • With what input? Commented Apr 14, 2017 at 13:23
  • current_affect. Using the current_affect I want to see which of the two lists for this specific current_affect has greater score and then to return the correspondant 1, 0, -1. I have posted some code which just check my lists for all the cases. The problem is that the zip thing misses the real link between the current_affect since the lists do not contain the same current_affect so thus I want to add all of them in order to be able to compare for all cases. Commented Apr 14, 2017 at 13:26

2 Answers 2

1
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
Sign up to request clarification or add additional context in comments.

5 Comments

I am testing your example and I am receiving: TypeError: 'zip' object is not subscriptable
I tested it in Python 2. Check this stackoverflow.com/questions/27431390/…
Yes it seems that it is a dif between python 2 and 3.
Is it also xrange and range the same thing? I got issues also with xrange.
They are similar. range creates the entire list in memory while xrange doesn't.
1

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)

1 Comment

Probably I wasnt really clear enough. I have two lists list_A and list_B every lists contain 4 variables (level, current_affect, score, confidence). The variable for this example is constant and take the same value so I dont care for it. Therefore, by taking into account the current_affect variable I want to link the two lists list_A and list_B. Ideally I want to link them for every different current_affect {A, B, C, D, E, F} even in the case that is empty in the initial lists (so in that case I want to initialize it with score=0 and confidence =0).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.