1

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.?

sfjac
  • 7,119
  • 5
  • 45
  • 69

1 Answers1

3

Using random.sample on readlines:

import random

random.sample(open('foo.txt', 'r').readlines(), 3)
Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
  • Is it the most efficient method? especially if there is a huge amount of data? Thanks in advance – Noah16 May 09 '19 at 13:53
  • @Noah16 For huge files, you would probably want [reservoir sampling](https://stackoverflow.com/questions/2612648/reservoir-sampling). – Ami Tavory May 09 '19 at 16:29