Sep-05-2019, 10:27 PM
You could implement a list comprehension instead of list.extend().
You could also sort data and use the bisect module to identify and target specific indices to be checked. Bisecting works by dividing the list in half over and over again until you find a match. Instead of iterating over 2.3 million items in data, you be iterating over log 2 of 2.3 million - only 21 items!
This *should* work but may need some fine tuning. Again, data needs to be sorted first. There are some highly efficient sorting algorithms that should
be able to the do the trick.
data3 = [data2[index+1],data2[index+2],data2[index+3] for i in data for index, j in enumerate(data2) if i==j]List comprehensions are faster and you wouldn't be issuing a method call for every match.
You could also sort data and use the bisect module to identify and target specific indices to be checked. Bisecting works by dividing the list in half over and over again until you find a match. Instead of iterating over 2.3 million items in data, you be iterating over log 2 of 2.3 million - only 21 items!
This *should* work but may need some fine tuning. Again, data needs to be sorted first. There are some highly efficient sorting algorithms that should
be able to the do the trick.
import bisect for i, x in enumerate(data2): index = bisect.bisect_left(data, x) for y in data[index:]: if x != y: break data3.extend([data2[i+1],data2[i+2],data2[i+3]])