Skip to main content
 首页 » 编程设计

python之比较我的 python 中的两个列表

2024年10月01日8kenshinobiy

我有一个 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,第三个对应于变量score。因此,我想要的是通过了解我的方法输入 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']] 

然后压缩我的列表并执行比较。

请您参考如下方法:

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