我有一个python函数,它有两个变量,即所谓的 current_level 和 current_affect 。该函数使用训练数据计算一些规则,这些规则可以使用 current_level 和 current_affect 这两个变量来计算决策。该代码包含两个列表 list_A 和 list_B 。这些清单包含以下内容:
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']]
列表的第一个元素对应 current_level ,第二个元素对应 current_affect ,第三个元素对应变量得分。因此,我想知道我的 current_affect 方法的输入,以找出这个特定情况的两个列表中哪一个具有更高的分数,并且在list_A和-1的情况下返回相应的值1 list_B的情况或在相等的情况下为零。变量 current_affect 取A-F之间的值。首先,如何在列表中添加不存在的 current_affect 值。例如,list_A缺少级别A和B而list_B缺少级别A(我想用零添加子列表),最后,我如何计算返回值?
我的功能如下:
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")
编辑:我想检查哪些列表丢失,并将它们添加到列表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']]
然后压缩我的列表并进行比较。
答案 0 :(得分:1)
我会尝试将这些列表转换为更适合查找内容的数据结构,例如词典。这有点乱,但是:
Binding="{Binding MakeItemsLarge, RelativeSource={RelativeSource AncestorType=Page}}"
答案 1 :(得分: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
结果:
0
-1
1
1
0
0