按第二个嵌套列表中的值过滤嵌套列表

时间:2018-02-19 22:33:09

标签: python filtering nested-lists

我有两组包含类似信息的嵌套列表:

listA = [('1.140038401', 1096, 3.75, 3.9, 47999, 1.95, 2.06, 58805, 3.85, 4.6), ('1.140334422', 1096, 1.24, 1.37, 56301, 10.5, 90.0, 58805, 5.9, 10.5), ('1.139782842', 1141, 2.22, 2.24, 25422, 3.75, 3.85, 58805, 3.4, 3.45), ('1.140334234', 1141, 5.4, 8.6, 48224, 1.59, 1.79, 58805, 3.95, 5.4), ('1.140333219', 1703, 2.78, 3.3, 48461, 2.58, 3.05, 58805, 3.25, 4.0), ('1.139782512', 1703, 1.98, 2.0, 78984, 4.9, 5.0, 58805, 3.35, 3.4), ('1.140039878', 18567, 5.0, 6.8, 1096, 1.7, 1.8, 58805, 3.75, 4.5), ('1.139782182', 18567, 2.24, 2.28, 62683, 3.95, 4.1, 58805, 3.2, 3.25), ('1.140333386', 25422, 2.44, 2.86, 58943, 2.98, 3.65, 58805, 3.2, 3.95), ('1.140040066', 47999, 1.6, 1.71, 55190, 4.8, 7.4, 58805, 4.1, 6.0), ('1.140039300', 48224, 1.23, 1.25, 78984, 19.5, 22.0, 58805, 6.8, 7.4), ('1.139783364', 48351, 2.16, 2.22, 55190, 3.9, 4.1, 58805, 3.4, 3.5), ('1.140333033', 48351, 2.18, 2.42, 56323, 3.5, 4.2, 58805, 3.35, 3.95), ('1.140038782', 48461, 1.95, 2.12, 1141, 4.0, 4.8, 58805, 3.55, 4.3), ('1.139783176', 48461, 1.79, 1.81, 69720, 5.4, 5.5, 58805, 3.85, 3.95), ('1.140333894', 48756, 2.2, 2.64, 56299, 3.25, 4.3, 58805, 3.2, 4.3), ('1.139783552', 52689, 7.2, 7.6, 48224, 1.55, 1.57, 58805, 4.4, 4.6), ('1.140040261', 52689, 6.2, 7.2, 48351, 1.61, 1.71, 58805, 3.85, 4.8), ('1.140334059', 55190, 1.32, 1.46, 52689, 9.2, 50.0, 58805, 4.9, 7.6), ('1.140038590', 56299, 2.62, 2.94, 56343, 2.88, 3.35, 58805, 3.0, 3.2), ('1.139782347', 56299, 2.92, 2.98, 58943, 2.9, 2.94, 58805, 3.15, 3.2), ('1.140039520', 56301, 2.2, 2.42, 1703, 3.6, 4.2, 58805, 3.25, 3.7), ('1.139783007', 56301, 2.4, 2.46, 56343, 3.3, 3.4, 58805, 3.4, 3.45), ('1.140039686', 56323, 1.25, 1.31, 25422, 12.0, 40.0, 58805, 6.2, 9.8), ('1.139782677', 56323, 1.25, 1.26, 48756, 14.5, 15.0, 58805, 7.4, 7.6), ('1.140333722', 56343, 1.84, 2.06, 18567, 4.5, 6.0, 58805, 3.45, 4.3), ('1.140038951', 58943, 1.83, 1.93, 69720, 4.5, 5.5, 58805, 3.65, 4.2), ('1.140039119', 62683, 2.36, 2.58, 48756, 3.25, 3.7, 58805, 3.25, 3.4), ('1.140334610', 69720, 10.5, 90.0, 47999, 1.24, 1.37, 58805, 5.9, 10.5), ('1.140333552', 78984, 2.78, 3.3, 62683, 2.6, 3.1, 58805, 3.15, 3.95)]

listB = [(1096, 47999, '3.90', '1.95', '4.15'), (1096, 56301, '1.28', '10.50', '5.50'), (1141, 25422, '2.20', '3.70', '3.44'), (1141, 48224, '5.50', '1.60', '4.00'), (1703, 48461, '2.80', '2.60', '3.25'), (1703, 78984, '1.97', '5.00', '3.30'), (18567, 1096, '5.25', '1.70', '4.04'), (18567, 62683, '2.20', '4.02', '3.25'), (25422, 58943, '2.40', '3.00', '3.20'), (47999, 55190, '1.64', '5.82', '4.30'), (48224, 78984, '1.23', '19.00', '7.00'), (48351, 55190, '2.16', '3.88', '3.40'), (48351, 56323, '2.20', '3.40', '3.35'), (48461, 1141, '1.97', '4.00', '3.75'), (48461, 69720, '1.75', '5.40', '3.95'), (48756, 56299, '2.30', '3.30', '3.20'), (52689, 48351, '6.25', '1.65', '4.05'), (55190, 52689, '1.35', '9.50', '5.00'), (56299, 56343, '2.75', '3.00', '3.10'), (56299, 58943, '2.92', '2.84', '3.10'), (56301, 1703, '2.25', '3.69', '3.29'), (56301, 56343, '2.38', '3.37', '3.40'), (56323, 25422, '1.25', '13.00', '6.60'), (56323, 48756, '1.25', '14.50', '7.55'), (56343, 18567, '1.83', '4.50', '3.40'), (58943, 69720, '1.83', '4.80', '3.70'), (62683, 48756, '2.40', '3.31', '3.27'), (69720, 47999, '10.50', '1.28', '5.50'), (78984, 62683, '2.80', '2.65', '3.10')]

我正在尝试生成一个新的嵌套列表(或修改现有的列表),以便它包含listA的所有元素,其中:

  • listA中的元素[1]和[4]分别匹配listB中的元素[0]和[1]。

到目前为止,我已尝试过这段代码:

newListA=[]
for i in range(0, len(listB)):
    for j in range(0, len(listB)):
        if listA[i][1] == listB[j][0] and listA[i][4] == listB[j][1]:
            newListA.append(listA[j])

这不会产生所需的结果,该结果应该是包含listA的所有元素的列表,除了一个:

('1.139783552', 52689, 7.2, 7.6, 48224, 1.55, 1.57, 58805, 4.4, 4.6)

1 个答案:

答案 0 :(得分:3)

要以合理的时间效率执行此操作,请使用listB中的相关项创建一组元组:

listb_hash = {(a,b) for a,b,*_ in listB}

然后创建新列表:

new_list = [x for x in listA if (x[1], x[4]) in listb_hash]
print(len(new_list), len(listA))

输出:

29 30

这是一张精美的new_list

[('1.140038401', 1096, 3.75, 3.9, 47999, 1.95, 2.06, 58805, 3.85, 4.6),
 ('1.140334422', 1096, 1.24, 1.37, 56301, 10.5, 90.0, 58805, 5.9, 10.5),
 ('1.139782842', 1141, 2.22, 2.24, 25422, 3.75, 3.85, 58805, 3.4, 3.45),
 ('1.140334234', 1141, 5.4, 8.6, 48224, 1.59, 1.79, 58805, 3.95, 5.4),
 ('1.140333219', 1703, 2.78, 3.3, 48461, 2.58, 3.05, 58805, 3.25, 4.0),
 ('1.139782512', 1703, 1.98, 2.0, 78984, 4.9, 5.0, 58805, 3.35, 3.4),
 ('1.140039878', 18567, 5.0, 6.8, 1096, 1.7, 1.8, 58805, 3.75, 4.5),
 ('1.139782182', 18567, 2.24, 2.28, 62683, 3.95, 4.1, 58805, 3.2, 3.25),
 ('1.140333386', 25422, 2.44, 2.86, 58943, 2.98, 3.65, 58805, 3.2, 3.95),
 ('1.140040066', 47999, 1.6, 1.71, 55190, 4.8, 7.4, 58805, 4.1, 6.0),
 ('1.140039300', 48224, 1.23, 1.25, 78984, 19.5, 22.0, 58805, 6.8, 7.4),
 ('1.139783364', 48351, 2.16, 2.22, 55190, 3.9, 4.1, 58805, 3.4, 3.5),
 ('1.140333033', 48351, 2.18, 2.42, 56323, 3.5, 4.2, 58805, 3.35, 3.95),
 ('1.140038782', 48461, 1.95, 2.12, 1141, 4.0, 4.8, 58805, 3.55, 4.3),
 ('1.139783176', 48461, 1.79, 1.81, 69720, 5.4, 5.5, 58805, 3.85, 3.95),
 ('1.140333894', 48756, 2.2, 2.64, 56299, 3.25, 4.3, 58805, 3.2, 4.3),
 ('1.140040261', 52689, 6.2, 7.2, 48351, 1.61, 1.71, 58805, 3.85, 4.8),
 ('1.140334059', 55190, 1.32, 1.46, 52689, 9.2, 50.0, 58805, 4.9, 7.6),
 ('1.140038590', 56299, 2.62, 2.94, 56343, 2.88, 3.35, 58805, 3.0, 3.2),
 ('1.139782347', 56299, 2.92, 2.98, 58943, 2.9, 2.94, 58805, 3.15, 3.2),
 ('1.140039520', 56301, 2.2, 2.42, 1703, 3.6, 4.2, 58805, 3.25, 3.7),
 ('1.139783007', 56301, 2.4, 2.46, 56343, 3.3, 3.4, 58805, 3.4, 3.45),
 ('1.140039686', 56323, 1.25, 1.31, 25422, 12.0, 40.0, 58805, 6.2, 9.8),
 ('1.139782677', 56323, 1.25, 1.26, 48756, 14.5, 15.0, 58805, 7.4, 7.6),
 ('1.140333722', 56343, 1.84, 2.06, 18567, 4.5, 6.0, 58805, 3.45, 4.3),
 ('1.140038951', 58943, 1.83, 1.93, 69720, 4.5, 5.5, 58805, 3.65, 4.2),
 ('1.140039119', 62683, 2.36, 2.58, 48756, 3.25, 3.7, 58805, 3.25, 3.4),
 ('1.140334610', 69720, 10.5, 90.0, 47999, 1.24, 1.37, 58805, 5.9, 10.5),
 ('1.140333552', 78984, 2.78, 3.3, 62683, 2.6, 3.1, 58805, 3.15, 3.95)]