python - Efficient way to compare sublists across lists of lists? -
sorry bad phrasing of title(which sheds light on, if not excuses me for, googling failures...). hope explanation clear enough.
i have 2 lists of lists(lines on), , want check cases whenever 2 lines across lists fit each other based on criteria. way know doing in double loop, is, well, slow. sorting them(lists) first not option, because lists not consistent each other.
edit:
ok, here how 2 lists like. they're 10.000 sublists long; tried best mimic characteristics , relations each other(different lenghts, sublists have match(by date , 2 capitalized elements) in other list, though don't) below.
['xyz', 'xyz', '12/11/2006', 'zatgxg', 'fuietg', '3'], ['xyz', 'xyz', '23/04/2011', 'gcatia', 'cecfoz', '0'], ['xyz', 'xyz', '08/03/2003', 'fuietg', 'erzhgg', '2'], ['xyz', 'xyz', '07/05/2006', 'aapoaa', 'fuietg', '1'], ['xyz', 'xyz', '15/05/2004', 'bfaext', 'eghege', '1'], ['xyz', 'xyz', '05/02/2006', 'gtoadr', 'udpfdf', '1'], ['xyz', 'xyz', '11/09/2004', 'racdgo', 'zchxgx', '0'], ['xyz', 'xyz', '03/04/2011', 'zdcfii', 'rhiiog', '1'], ['xyz', 'xyz', '07/04/2007', 'dabzgi', 'gpeiot', '4'], ['xyz', 'xyz', '16/03/2008', 'dohbur', 'oucegh', '2'] ################## ['xyz', 'xyz', 'dohbur', 'oucegh', 'xyz', 'xyz', '16/03/2008'], ['xyz', 'xyz', 'dabzgi', 'gpeiot', 'xyz', 'xyz', '07/04/2007'], ['xyz', 'xyz', 'fuietg', 'erzhgg', 'xyz', 'xyz', '08/03/2003'], ['xyz', 'xyz', 'udioac', 'gceabb', 'xyz', 'xyz', '21/02/2004'], ['xyz', 'xyz', 'bfaext', 'eghege', 'xyz', 'xyz', '15/05/2004'], ['xyz', 'xyz', 'racdgo', 'zchxgx', 'xyz', 'xyz', '11/09/2004'], ['xyz', 'xyz', 'gtoadr', 'udpfdf', 'xyz', 'xyz', '05/02/2006'], ['xyz', 'xyz', 'aapoaa', 'fuietg', 'xyz', 'xyz', '07/05/2006']
from question, gather have 2 lists, of same size , ordered in fashion must not change.
you want know if item list present in list b.
you can make sort keeps reference initial index, allows search using bisect more quickly.
see how indices of sorted array in python
numpy.argsort
that, too
edit
from comment, trivial solution (see common elements comparison between 2 lists)
>>> list1 = [1,2,3,4,5,6] >>> list2 = [3, 5, 7, 9] >>> list(set(list1).intersection(list2)) [3, 5]
i point out highly efficient testing , inserting in set o(1)
operations, making whole operation o(n+m)
)
(in example, lists sorted, that's coincidence)
more edit, based on examples
if need matching elements, simple revision of previous :
list(set([x[0:4] x in list1]).intersection([(x[0], x[1], x[2], x[3], x[6]) x in list2]))
the comprehensions reduce list1 , list2 common elements in expected order. if need elements both lists, may slighly more complex.
Comments
Post a Comment