Compare two lists in my python

I have a python function that has the so-called current_level and current_affect as input two variables . The function calculates some rules using training data, and these rules can calculate decisions using these two variables: current_level and current_affect . The code contained two lists list_A and list_B . The lists included 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 list corresponds to current_level , the second to current_affect , and the third to the score variable . Therefore, I want to know how to introduce my method for current_affect , to find which of the two lists for this particular case has a higher score and returns the corresponding value 1 in the case of list_A and -1 for the case list_B or zero in case of equality. The variable current_affect takes values ​​between AF. First, how can I add nonexistent current_affect values to my lists. For example, list_A is missing levels A and B, while list_B is missing level A (I want to add sublists with zeros) and finally, how can I calculate the return value?

My function is this:

def association_rules_adaptation(level, current_affect):

:

res = zip(list_A,list_B)

zip. current_affect, , current_affect. , current_affect, :

(["'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'])

, , , .

    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: , , list_A list_B, :

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']]

, .

+4
2
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

:

0
-1
1
1
0
0
+1

, , . , :

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

Source: https://habr.com/ru/post/1674831/


All Articles