loops - Python - most efficient way of matching a list -
say example have list of 7 numbers, let's call 'linea' , have 50 lines of numbers below , want see if 'linea' matches 50 lines in order. quickest (time-wise)/most efficient way of doing it? loop? or other method?
linea = [1,2,3,4,5,6,7] linetwo = [1,33,40,44,45,1,2] linethree = [2,13,22,41,50,8,9] linefour = [1,2,3,4,5,6,7] linefive = etc.....(repeat 50 times)
thank you
you can compare 2 lists ==
operator:
if linea == lineone: print 'they match!'
now, keep lines in list:
lines = [lineone, linetwo, linethree, ..., linefifty]
and find lines match:
matches = [line line in lines if line == linea]
you can't more efficient comparing each line (o(n)
). unless sort input first. use bisect
module , o(log n)
performance. if want compare lineb, linec, linez
lines
well. otherwise don't bother, because sorting compare lines o(n * log n)
...
Comments
Post a Comment