这是大型计划的一部分。我有一个像
这样的清单cnfn=[(1, -3), (2, -3), (-1, -2, 3), (-1, 4), (-2, 4), (1, 2, -4), (-4, -5), (4, 5), (-3, 6), (-5, 6), (3, 5, -6), (7, -8), (6, -8), (-7, -6, 8), (-6, 9), (-7, 9), (6, 7, -9), (-9, -10), (9, 10), (-8, 11), (-10, 11), (8, 10, -11), (7, -12), (4, -12), (-7, -4, 12), (-12, 13), (-3, 13), (12, 3, -13), (14, -16), (15, -16), (-14, -15, 16), (-16, -17), (16, 17), (-14, 18), (-15, 18), (14, 15, -18), (17, -19), (18, -19), (-17, -18, 19), (13, -20), (19, -20), (-13, -19, 20), (-20, -21), (20, 21), (-19, 22), (-13, 22), (19, 13, -22), (21, -23), (22, -23), (-21, -22, 23), (13, -24), (18, -24), (-13, -18, 24), (-24, 25), (-16, 25), (24, 16, -25), (26, -28), (27, -28), (-26, -27, 28), (-28, -29), (28, 29), (-26, 30), (-27, 30), (26, 27, -30), (29, -31), (30, -31), (-29, -30, 31), (25, -32), (31, -32), (-25, -31, 32), (-32, -33), (32, 33), (-31, 34), (-25, 34), (31, 25, -34), (33, -35), (34, -35), (-33, -34, 35), (25, -36), (30, -36), (-25, -30, 36), (-36, 37), (-28, 37), (36, 28, -37), (38, -40), (39, -40), (-38, -39, 40), (-40, -41), (40, 41), (-38, 42), (-39, 42), (38, 39, -42), (41, -43), (42, -43), (-41, -42, 43), (37, -44), (43, -44), (-37, -43, 44), (-44, -45), (44, 45), (-43, 46), (-37, 46), (43, 37, -46), (45, -47), (46, -47), (-45, -46, 47), (37, -48), (42, -48), (-37, -42, 48), (-48, 49), (-40, 49), (48, 40, -49), (-50, -51), (50, 51), (-51, 53), (-52, 53), (51, 52, -53), (-52, -54), (52, 54), (-54, 55), (-50, 55), (54, 50, -55), (53, -56), (55, -56), (-53, -55, 56), (-56, -57), (56, 57), (58, -59), (57, -59), (-58, -57, 59), (52, -60), (50, -60), (-52, -50, 60), (-59, 61), (-60, 61), (59, 60, -61), (56, -62), (58, -62), (-56, -58, 62), (-58, -63), (58, 63), (57, -64), (63, -64), (-57, -63, 64), (-62, 65), (-64, 65), (62, 64, -65), (-66, -67), (66, 67), (-67, 69), (-68, 69), (67, 68, -69), (-68, -70), (68, 70), (-70, 71), (-66, 71), (70, 66, -71), (69, -72), (71, -72), (-69, -71, 72), (-72, -73), (72, 73), (61, -74), (73, -74), (-61, -73, 74), (68, -75), (66, -75), (-68, -66, 75), (-74, 76), (-75, 76), (74, 75, -76), (72, -77), (61, -77), (-72, -61, 77), (-61, -78), (61, 78), (73, -79), (78, -79), (-73, -78, 79), (-77, 80), (-79, 80), (77, 79, -80), (-81, -82), (81, 82), (-82, 84), (-83, 84), (82, 83, -84), (-83, -85), (83, 85), (-85, 86), (-81, 86), (85, 81, -86), (84, -87), (86, -87), (-84, -86, 87), (-87, -88), (87, 88), (76, -89), (88, -89), (-76, -88, 89), (83, -90), (81, -90), (-83, -81, 90), (-89, 91), (-90, 91), (89, 90, -91), (87, -92), (76, -92), (-87, -76, 92), (-76, -93), (76, 93), (88, -94), (93, -94), (-88, -93, 94), (-92, 95), (-94, 95), (92, 94, -95), (-96, -97), (96, 97), (-97, 99), (-98, 99), (97, 98, -99), (-98, -100), (98, 100), (-100, 101), (-96, 101), (100, 96, -101), (99, -102), (101, -102), (-99, -101, 102), (-102, -103), (102, 103), (91, -104), (103, -104), (-91, -103, 104), (-104, -105), (104, 105), (-104, 106), (-105, 106), (104, 105, -106), (102, -107), (91, -107), (-102, -91, 107), (-91, -108), (91, 108), (103, -109), (108, -109), (-103, -108, 109), (-107, 110), (-109, 110), (107, 109, -110), (-1, 50), (1, -50), (-2, 52), (2, -52), (-7, 58), (7, -58), (-14, 66), (14, -66), (-15, 68), (15, -68), (-26, 81), (26, -81), (-27, 83), (27, -83), (-38, 96), (38, -96), (-39, 98), (39, -98), (-11, -65, -111), (-11, 65, 111), (11, -65, 111), (11, 65, -111), (-23, -80, -112), (-23, 80, 112), (23, -80, 112), (23, 80, -112), (-35, -95, -113), (-35, 95, 113), (35, -95, 113), (35, 95, -113), (-47, -106, -114), (-47, 106, 114), (47, -106, 114), (47, 106, -114), (-49, -110, -115), (-49, 110, 115), (49, -110, 115), (49, 110, -115), (111, 112, 113, 114, 115)]
还有另一个清单
cnfb=[(1, -3), (2, -3), (-1, -2, 3), (-1, 4), (-2, 4), (1, 2, -4), (4, 5), (-4, -5), (-3, 6), (-5, 6), (3, 5, -6), (7, -8), (6, -8), (-7, -6, 8), (-6, 9), (-7, 9), (6, 7, -9), (9, 10), (-9, -10), (-8, 11), (-10, 11), (8, 10, -11), (7, -12), (4, -12), (-7, -4, 12), (-12, 13), (-3, 13), (12, 3, -13), (14, -16), (15, -16), (-14, -15, 16), (16, 17), (-16, -17), (-14, 18), (-15, 18), (14, 15, -18), (17, -19), (18, -19), (-17, -18, 19), (13, -20), (19, -20), (-13, -19, 20), (20, 21), (-20, -21), (-19, 22), (-13, 22), (19, 13, -22), (21, -23), (22, -23), (-21, -22, 23), (13, -24), (18, -24), (-13, -18, 24), (-24, 25), (-16, 25), (24, 16, -25), (26, -28), (27, -28), (-26, -27, 28), (28, 29), (-28, -29), (-26, 30), (-27, 30), (26, 27, -30), (29, -31), (30, -31), (-29, -30, 31), (25, -32), (31, -32), (-25, -31, 32), (32, 33), (-32, -33), (-31, 34), (-25, 34), (31, 25, -34), (33, -35), (34, -35), (-33, -34, 35), (25, -36), (30, -36), (-25, -30, 36), (-36, 37), (-28, 37), (36, 28, -37), (38, -40), (39, -40), (-38, -39, 40), (40, 41), (-40, -41), (-38, 42), (-39, 42), (38, 39, -42), (41, -43), (42, -43), (-41, -42, 43), (37, -44), (43, -44), (-37, -43, 44), (44, 45), (-44, -45), (-43, 46), (-37, 46), (43, 37, -46), (45, -47), (46, -47), (-45, -46, 47), (37, -48), (42, -48), (-37, -42, 48), (-48, 49), (-40, 49), (48, 40, -49), (50, 51), (-50, -51), (-51, 53), (-52, 53), (51, 52, -53), (52, 54), (-52, -54), (-54, 55), (-50, 55), (54, 50, -55), (53, -56), (55, -56), (-53, -55, 56), (56, 57), (-56, -57), (58, -59), (57, -59), (-58, -57, 59), (52, -60), (50, -60), (-52, -50, 60), (-59, 61), (-60, 61), (59, 60, -61), (56, -62), (58, -62), (-56, -58, 62), (58, 63), (-58, -63), (57, -64), (63, -64), (-57, -63, 64), (-62, 65), (-64, 65), (62, 64, -65), (66, 67), (-66, -67), (-67, 69), (-68, 69), (67, 68, -69), (68, 70), (-68, -70), (-70, 71), (-66, 71), (70, 66, -71), (69, -72), (71, -72), (-69, -71, 72), (72, 73), (-72, -73), (61, -74), (73, -74), (-61, -73, 74), (68, -75), (66, -75), (-68, -66, 75), (-74, 76), (-75, 76), (74, 75, -76), (72, -77), (61, -77), (-72, -61, 77), (61, 78), (-61, -78), (73, -79), (78, -79), (-73, -78, 79), (-77, 80), (-79, 80), (77, 79, -80), (81, 82), (-81, -82), (-82, 84), (-83, 84), (82, 83, -84), (83, 85), (-83, -85), (-85, 86), (-81, 86), (85, 81, -86), (84, -87), (86, -87), (-84, -86, 87), (87, 88), (-87, -88), (76, -89), (88, -89), (-76, -88, 89), (83, -90), (81, -90), (-83, -81, 90), (-89, 91), (-90, 91), (89, 90, -91), (87, -92), (76, -92), (-87, -76, 92), (76, 93), (-76, -93), (88, -94), (93, -94), (-88, -93, 94), (-92, 95), (-94, 95), (92, 94, -95), (96, 97), (-96, -97), (-97, 99), (-98, 99), (97, 98, -99), (98, 100), (-98, -100), (-100, 101), (-96, 101), (100, 96, -101), (99, -102), (101, -102), (-99, -101, 102), (102, 103), (-102, -103), (91, -104), (103, -104), (-91, -103, 104), (104, 105), (-104, -105), (-104, 106), (-105, 106), (104, 105, -106), (102, -107), (91, -107), (-102, -91, 107), (91, 108), (-91, -108), (103, -109), (108, -109), (-103, -108, 109), (-107, 110), (-109, 110), (107, 109, -110), (35, 95, -111), (-35, -95, -111), (-35, 95, 111), (35, -95, 111), (23, 80, -112), (-23, -80, -112), (-23, 80, 112), (23, -80, 112), (49, 106, -113), (-49, -106, -113), (-49, 106, 113), (49, -106, 113), (47, 110, -114), (-47, -110, -114), (-47, 110, 114), (47, -110, 114), (11, 65, -115), (-11, -65, -115), (-11, 65, 115), (11, -65, 115), [111, 112, 113, 114, 115], (-26, 83), (26, -83), (-2, 50), (2, -50), (-38, 98), (38, -98), (-27, 81), (27, -81), (-39, 96), (39, -96), (-7, 58), (7, -58), (-14, 68), (14, -68), (-15, 66), (15, -66), (-1, 52), (1, -52)]
如果我用平面眼检查看起来有相同的值,但如果我把它们放在同一个函数中,结果会有所不同。如何确定这两者具有完全相同的类型和相同的值?
答案 0 :(得分:2)
两个lists
不相同。这就是为什么function
可能会为不同的lists
提供不同的结果。
要检查,如果 2
lists
相同,您可以执行以下操作:
list1 == list2
所以举一些例子:
>>> [1, 2, 3, 4, 5] == [1, 2, 3, 4, 5]
True
>>> [1, 2, 3, 4, 5] == [1, 2, 3, 4, 3]
False
>>> [1, 2, 3, 4, 5] == [5, 4, 3, 2, 1]
False
>>> [(1, 2), (3, 4)] == [(1, 2), (3, 4)]
True
>>> [(1, 2), (3, 4)] == [(1, 2), (3, 5)]
False
如果您想查找differences
的内容,可以执行以下操作:
[e for e in list1 if e not in list2] + [e for e in list2 if e not in list1]
我认为实际上非常可读。
所以我们可以把它放在function
:
def comp(list1, list2):
return [e for e in list1 if e not in list2] + [e for e in list2 if e not in list1]
和一些例子:
>>> comp([1, 2, 3], [1, 2, 3]) #should be empty as no differnence
[]
>>> comp([(1, 2), (3, 4)], [(1, 2), (3, 5)])
[(3, 4), (3, 5)]
>>> comp([(1, 2), (3, 4)], [(1, 2), (3, 5), (6, 7)])
[(3, 4), (3, 5), (6, 7)]