比较列表中的值

时间:2015-11-27 09:48:16

标签: python list compare

我的列表如下所示:

abc = 
[
 [ 
   [True, 08JUN2015 00:00, 13JUN2015 00:00, False], 
   [True, 06JUL2015 00:00, 11JUL2015 00:00, True, A, B, C], 
   [True, 28SEP2015 00:00, 03OCT2015 00:00, True, P, Q, R]
 ], 
 [ 
   [True, 18MAY2015 00:00, 23MAY2015 00:00, True, A, B, C], 
   [True, 29DEC2014 00:00, 03JAN2015 00:00, False], 
   [True, 21SEP2015 00:00, 26SEP2015 00:00, True, X, Y, Z]  
 ], 
 [
   [True, 13APR2015 00:00, 18APR2015 00:00, True, D, E, F], 
   [True, 21DEC2015 00:00, 26DEC2015 00:00, True, P, Q, R], 
   [False, 06APR2015 00:00, 11APR2015 00:00, True, Z, U, Y]
 ], 
 [
   [True, 13JUL2015 00:00, 18JUL2015 00:00, True, A,B,C], 
   [True, 09NOV2015 00:00, 14NOV2015 00:00, False], 
   [True, 05JAN2015 00:00, 10JAN2015 00:00, True, A, X, Z], 
   [False, 19OCT2015 00:00, 24OCT2015 00:00, True, B, Z, A], 
   [True, 20JUL2015 00:00, 25JUL2015 00:00, True, E, F, R]  
 ], 
 [
   [True, 23FEB2015 00:00, 28FEB2015 00:00, True, E, R, N],  
   [True, 07DEC2015 00:00, 12DEC2015 00:00, False, W, T, P], 
   [True, 20JUL2015 00:00, 25JUL2015 00:00, True, R, U, Y]
 ]
 [
   [True, 23FEB2015 00:00, 28FEB2015 00:00, True, A, V, D],  
   [True, 07DEC2015 00:00, 12DEC2015 00:00, True, E, Q, R], 
   [True, 20JUL2015 00:00, 25JUL2015 00:00, True, W, R, X]
 ]
]

比较列表和签入列表的最佳方法是列出第一个可用记录,其中列表的第一个元素(Bool)等于列表的第四个元素(Bool值)为所有'True '值列在内部列表中。

所以,对于上面的列表,它应该在列表中检查 结果应该是列表abc中第一个可用的'list',其中所有第一个位置和第四个位置都是True,而不管第一个位置是否为假。

 result = [ 
       [True, 18MAY2015 00:00, 23MAY2015 00:00, True, A, B, C], 
       [True, 29DEC2014 00:00, 03JAN2015 00:00, False], 
       [True, 21SEP2015 00:00, 26SEP2015 00:00, True, X, Y, Z]  
     ]

仅检查并比较列表中的“True”字段,并使用True返回列表中的所有值。

2 个答案:

答案 0 :(得分:3)

此代码有效:

from itertools import chain

the_list = [
 [ 
   [True, "08JUN2015 00:00", "13JUN2015 00:00", False], 
   [True, "06JUL2015 00:00", "11JUL2015 00:00", True, 1309024584, 'V4YK+lysQgeLXVx9TrkzxQ==', "05JUL2015 00:00"], 
   [True, "28SEP2015 00:00", "03OCT2015 00:00", True, 1309024632, 'lYDIi9ieSoy8nVdVeiBk8Q==', "27SEP2015 00:00"]
 ], 

 [ 
   [True, "18MAY2015 00:00", "23MAY2015 00:00", True, 1309024556, 'lDp+xHboQ2u8NmNTrDcHug==', "17MAY2015 00:00"], 
   [True, "29DEC2014 00:00", "03JAN2015 00:00", False], 
   [True, "21SEP2015 00:00", "26SEP2015 00:00", True, 1309024628, 'BVNYf4MyT0S+PBjFWBUq1A==', "20SEP2015 00:00"]  
 ], 

 [
   [True, "13APR2015 00:00", "18APR2015 00:00", True, 1309024536, 'pwuzfaUuQrm6n00oBoXHwA==', "12APR2015 00:00"], 
   [True, "21DEC2015 00:00", "26DEC2015 00:00", True, 1309024680, 'RmbuGHxbSvKZnh9z6BIj7A==', "20DEC2015 00:00"], 
   [False, "06APR2015 00:00", "11APR2015 00:00", False]
 ], 
]

print [x for x in chain(*the_list) if x[0] is True and x[3] is True]

<强>输出:

[
    [True, '06JUL2015 00:00', '11JUL2015 00:00', True, 1309024584, 'V4YK+lysQgeLXVx9TrkzxQ==', '05JUL2015 00:00'],
    [True, '28SEP2015 00:00', '03OCT2015 00:00', True, 1309024632, 'lYDIi9ieSoy8nVdVeiBk8Q==', '27SEP2015 00:00'],
    [True, '18MAY2015 00:00', '23MAY2015 00:00', True, 1309024556, 'lDp+xHboQ2u8NmNTrDcHug==', '17MAY2015 00:00'],
    [True, '21SEP2015 00:00', '26SEP2015 00:00', True, 1309024628, 'BVNYf4MyT0S+PBjFWBUq1A==', '20SEP2015 00:00'],
    [True, '13APR2015 00:00', '18APR2015 00:00', True, 1309024536, 'pwuzfaUuQrm6n00oBoXHwA==', '12APR2015 00:00'],
    [True, '21DEC2015 00:00', '26DEC2015 00:00', True, 1309024680, 'RmbuGHxbSvKZnh9z6BIj7A==', '20DEC2015 00:00']
]

答案 1 :(得分:0)

假设:

abc = [
 [ 
    [True, "08JUN2015 00:00", "13JUN2015 00:00", False], 
    [True, "06JUL2015 00:00", "11JUL2015 00:00", True, "A", "B", "C"], 
    [True, "28SEP2015 00:00", "03OCT2015 00:00", True, "P", "Q", "R"]
 ], 
 [ 
    [True, "18MAY2015 00:00", "23MAY2015 00:00", True, "A", "B", "C"], 
    [True, "29DEC2014 00:00", "03JAN2015 00:00", False], 
    [True, "21SEP2015 00:00", "26SEP2015 00:00", True, "X", "Y", "Z"]  
 ], 
 [
    [True, "13APR2015 00:00", "18APR2015 00:00", True, "D", "E", "F"], 
    [True, "21DEC2015 00:00", "26DEC2015 00:00", True, "P", "Q", "R"], 
    [False, "06APR2015 00:00", "11APR2015 00:00", True, "Z", "U", "Y"]
 ], 
 [
    [True, "13JUL2015 00:00", "18JUL2015 00:00", True, "A","B","C"], 
    [True, "09NOV2015 00:00", "14NOV2015 00:00", False], 
    [True, "05JAN2015 00:00", "10JAN2015 00:00", True, "A", "X", "Z"], 
    [False, "19OCT2015 00:00", "24OCT2015 00:00", True, "B", "Z", "A"], 
    [True, "20JUL2015 00:00", "25JUL2015 00:00", True, "E", "F", "R"]  
 ], 
 [
    [True, "23FEB2015 00:00", "28FEB2015 00:00", True, "E", "R", "N"],  
    [True, "07DEC2015 00:00", "12DEC2015 00:00", False, "W", "T", "P"], 
    [True, "20JUL2015 00:00", "25JUL2015 00:00", True, "R", "U", "Y"]
 ],
 [
    [True, "23FEB2015 00:00", "28FEB2015 00:00", True, "A", "V", "D"],  
    [True, "07DEC2015 00:00", "12DEC2015 00:00", True, "E", "Q", "R"], 
    [True, "20JUL2015 00:00", "25JUL2015 00:00", True, "W", "R", "X"]
 ]
]

您可以编写嵌套循环:

[sl for li in abc for sl in li if all(e is True for e in (sl[0], sl[3]))]

结果:

[[True, '06JUL2015 00:00', '11JUL2015 00:00', True, 'A', 'B', 'C'], 
 [True, '28SEP2015 00:00', '03OCT2015 00:00', True, 'P', 'Q', 'R'], 
 [True, '18MAY2015 00:00', '23MAY2015 00:00', True, 'A', 'B', 'C'], 
 [True, '21SEP2015 00:00', '26SEP2015 00:00', True, 'X', 'Y', 'Z'], 
 [True, '13APR2015 00:00', '18APR2015 00:00', True, 'D', 'E', 'F'], 
 [True, '21DEC2015 00:00', '26DEC2015 00:00', True, 'P', 'Q', 'R'], 
 [True, '13JUL2015 00:00', '18JUL2015 00:00', True, 'A', 'B', 'C'], 
 [True, '05JAN2015 00:00', '10JAN2015 00:00', True, 'A', 'X', 'Z'], 
 [True, '20JUL2015 00:00', '25JUL2015 00:00', True, 'E', 'F', 'R'], 
 [True, '23FEB2015 00:00', '28FEB2015 00:00', True, 'E', 'R', 'N'], 
 [True, '20JUL2015 00:00', '25JUL2015 00:00', True, 'R', 'U', 'Y'], 
 [True, '23FEB2015 00:00', '28FEB2015 00:00', True, 'A', 'V', 'D'], 
 [True, '07DEC2015 00:00', '12DEC2015 00:00', True, 'E', 'Q', 'R'], 
 [True, '20JUL2015 00:00', '25JUL2015 00:00', True, 'W', 'R', 'X']]