【问题标题】:How do you compare integer values in two separate lists?如何比较两个单独列表中的整数值?
【发布时间】:2015-01-08 19:30:26
【问题描述】:

这是我当前的代码:

predict_results = []

with open ("newTesting.predict") as inputfile:

    for line in inputfile:
        predict_results.append(line.strip())
print predict_results


first_list = []

with open ("newTesting") as inputfile:

    for line in inputfile:
        first_list.append(line.strip().split()[0])
print first_list


if predict_results [0] == first_list[0]:

    print True
else:
    print False

这是我当前的输出:

['-1', '-1', '-1', '-1', '-1', '-1', '-1', '-1', '-1', ' -1','1','-1','-1','-1','-1','-1','-1','-1','-1','1 ','1','-1','-1','-1','-1','-1','1','-1','-1','-1', '-1'、'-1'、'-1'、'1'、'-1'、'-1'、'-1'、'1'、'-1'、'1'、'-1 ','-1','-1','-1','-1','1','-1','1','-1','-1','-1', '-1'、'-1'、'-1'、'1'、'-1'、'1'、'-1'、'-1'、'-1'、'-1'、'- 1'、'-1'、'-1'、'-1'、'-1'、'-1'、'-1'、'-1'、'-1'、'-1'、'- 1','-1','1','-1','1','-1','1','-1','-1','-1','-1', '-1'、'-1'、'1'、'1'、'-1'、'1'、'-1'、'1'、'-1'、'1'、'-1'、 '-1'、'1'、'-1'、'-1'、'-1'、'-1'、'-1'、'1'、'-1'、'-1'、'- 1'、'1'、'1'、'-1'、'-1'、'-1'、'-1'、'-1'、'1'、'1'、'-1'、' -1','-1','-1','-1','-1','-1'] ['1', '-1', '1', '-1', '-1', '-1', '-1', '-1', '-1', '1', '1 '、'-1'、'1'、'1'、'1'、'1'、'-1'、'1'、'-1'、'1'、'1'、'1'、' 1'、'-1'、'1'、'-1'、'1'、'1'、'-1'、'-1'、'1'、'1'、'-1'、'1 '、'1'、'-1'、'1'、'1'、'1'、'1'、'-1'、'-1'、'1'、'1'、'1'、' 1'、'1'、'1'、'1'、'-1'、'-1'、'1'、'1'、'-1'、'1'、'-1'、'1' , '-1', '-1', '1', '1', '-1', '1', '1', '1', '-1', '-1', '-1' , '1', '-1', '-1', '1', '1', '1', '-1', '1', '-1', '-1', '-1' , '1', '-1', '-1', '-1', '1', '1', '-1', '-1', '-1', '-1', '- 1'、'1'、'1'、'-1'、'-1'、'1'、'-1'、'1'、'1'、'1'、'-1'、'1' , '-1', '-1', '-1', '1', '1', '-1', '1', '1', '-1', '-1', '-1 ', '-1', '-1', '1', '-1', '-1', '-1', '-1', '-1']

错误

我只能检查索引 [0] 是否正确。如何使用 first_list 检查 predict_results 中的所有索引

谢谢

【问题讨论】:

    标签: list python-2.7 indexing compare


    【解决方案1】:

    希望这会有所帮助:

    >>> from pandas import *
    
    >>> L1 = [1,2,3]
    >>> L2 = [2,2,3]
    
    >>> S1 = Series(L1)
    >>> S2 = Series(L2)
    
    >>> RES = S1==S2
    
    >>> RES
    0    False
    1     True
    2     True
    

    对于您的情况:

    >>> S1 = Series(predict_results)
    >>> S2 = Series(first_list)
    
    >>> RES = S1==S2
    
    >>> RES[0]   # check   predict_results[0]==first_list[0]
    >>> RES[1]   # check   predict_results[1]==first_list[1]
    

    你可以使用循环:

    for x,y in zip(predict_results,first_list):
        if(x==y):
            print False
        else:
            print True
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      相关资源
      最近更新 更多