I want to filter two list with any fastest method in python script. I have used the built-in filter() method for this purpose. but it is quite slow and taking too much time because I have very big list, I think more than 5 million item in each list or may be more.
I do not know how I will make it. Please if anybody have idea or write small function for it.
Maybe your lists are too large and do not fit in memory, and you experience thrashing. If the sources are in files, you do not need the whole list in memory all at once. Try using itertools, e.g.:
from itertools import ifilter
def is_important(s):
return len(s)>10
filtered_list = ifilter(is_important, open('mylist.txt'))
Note that ifilter returns an iterator that is fast and memory efficient.
Generator Tricks is a tutorial by David M. Beazley that teaches some interesting uses for generators.
If you can avoid creating the lists in the first place, you'll be happier.
Rather than
aBigList = someListMakingFunction()
filter( lambda x:x>10, aBigList )
You might want to look at your function that makes the list.
def someListMakingGenerator( ):
for x in some source:
yield x
Then your filter doesn't involve a giant tract of memory
def myFilter( aGenerator ):
for x in aGenerator:
if x > 10:
yield x
By using generators, you don't keep much stuff in memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With