I have a large file (5Gb) called my_file
. I have a list called my_list
. What is the most efficient way to read each line in the file and, if an item from my_list
matches an item from a line in my_file
, create a new list called matches
that contains items from the lines in my_file
AND items from my_list
where a match occurred. Here is what I am trying to do:
def calc(my_file, my_list)
matches = []
my_file.seek(0,0)
for i in my_file:
i = list(i.rstrip('\n').split('\t'))
for v in my_list:
if v[1] == i[2]:
item = v[0], i[1], i[3]
matches.append(item)
return matches
here are some lines in my_file
:
lion 4 blue ch3
sheep 1 red pq2
frog 9 green xd7
donkey 2 aqua zr8
here are some items in my_list
intel yellow
amd green
msi aqua
The desired output, a list of lists, in the above example would be:
[['amd', 9, 'xd7'], ['msi', 2, 'zr8']]
My code is currently work, albeit really slow. Would using a generator or serialization help? Thanks.
Keep the items in a dictional rather than a list (let's call it
items
). Now iterate through your file as you're doing and pick out the key to look for (i[2]
) and then check if it's there in the initems
.items would be.
So the checking part would be.
Since you're just creating the list and returning it, using a generator might help memory characteristics of the program rather than putting the whole thing into a list and returning it.
Here's a variation on @rocksportrocker's answer using
csv
module:Example:
There isn't much you can do with the overheads of reading the file in, but based on your example code, you can speed up the matching by storing your list as a dict (with the target field as the key).
Here's an example, with a few extra optimisation tweaks:
But again, do note that most of your performance bottleneck will be in the reading of the file and optimisation at this level may not have a big enough impact on the runtime.
You could build a dictonary for looking up v. I added further little optimizations:
This should be much faster for a large
my_list
.Using
get
is faster than checking ifi[2]
is invd
+ accessingvd[i[2]]
For getting more speedup beyond these optimizations I recommend http://www.cython.org