[Tutor] Find (list) strings in large textfile
Danny Yoo
dyoo at hashcollision.org
Sat Feb 11 19:59:52 EST 2017
Believe it or not, a change in two characters should make this even faster. :)
Change the line:
file_list = [i[:-1] for i in my_list.readlines()]
to:
file_list = {i[:-1] for i in my_list.readlines()}
The change is to use a "set comprehension" instead of a "list
comprehension". Sets allow membership checks in expected *constant*
time instead of *linear* time. Try that, and compare the speed: you
should get a fairly good speedup.
See:
https://docs.python.org/3/tutorial/datastructures.html#sets
for some more details.
More information about the Tutor
mailing list