I have the following method:
ids = random.sample(list(map(int, open(file_path))), 10)
that returns a list of 10 random ints.
How to speed up it? What is the other way to do this?
I have the following method:
ids = random.sample(list(map(int, open(file_path))), 10)
that returns a list of 10 random ints.
How to speed up it? What is the other way to do this?
it seems to me that the hangup is always going to be finding the length of the file. See How to get line count cheaply in Python? for that.
then you can do
import random
lines = random.sample(range(length_of_file), 10)
with open(file) as f:
lines_from_file = [f.readline(line) for line in lines]
or something like that. How does that compare speedwise?