I have a file like this:
Tree 5
Jaguar 9
Cat 23
Monkey 12
Gorilla 67
Is possible to randomly subsample 3 of these lines? For example:
Jaguar 9
Gorilla 67
Tree 5
or
Monkey 12
Tree 5
Cat 23
etc.?
I have a file like this:
Tree 5
Jaguar 9
Cat 23
Monkey 12
Gorilla 67
Is possible to randomly subsample 3 of these lines? For example:
Jaguar 9
Gorilla 67
Tree 5
or
Monkey 12
Tree 5
Cat 23
etc.?
Using random.sample
on readlines
:
import random
random.sample(open('foo.txt', 'r').readlines(), 3)