Compare two lists in my python -
i have python function has input 2 variables so-called current_level , current_affect. function calculates rules using training data , rules can calculating decisions using 2 variables current_level , current_affect. code contained 2 lists list_a , list_b. lists contained 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 lists correspond current_level, second current_affect , third variable score. therefore want knowing input of method current_affect find of 2 lists specific case has greater score , return correspondant value 1 in case of list_a , -1 case of list_b or 0 in equality case. variable current_affect takes values between a-f. firstly, how can add non-existent current_affect values in lists. example list_a missing levels , b while list_b missing level (i want add sublists zeros) , finally, how can calculate return value?
my function following:
def association_rules_adaptation(level, current_affect):
then in code have zipped 2 lists:
res = zip(list_a,list_b)
withe zip want link 2 lists regarding current_affect in order able compare list has greater score specific current_affect. problem when lists doesn not contained same values of current_affect 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 want add possible values in lists in order able link them correctly , perform comparisons.
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: want check lists missing , add them lists list_a , list_b, 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 zip lists , perform comparisons.
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']] = zip(*list_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 characters `c1` `c2`, inclusive.""" c in xrange(ord(c1), ord(c2)+1): yield chr(c) i=0 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) 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
Comments
Post a Comment