Reputation:
import os, glob, numpy as np
files = glob.glob('*.jpg')
indices = np.array([i for i,j in enumerate(files)])
selected_indices = np.random.choice(indices,500,replace=False)
print files[selected_indices]
I could not convert indices into files. What's good way of doing it?
Upvotes: 1
Views: 399
Reputation: 32189
The problem is that glob.glob('*.jpg')
returns a list and python does not support:
list[array_of_indices]
Instead, do it as:
selected_indices = list(np.random.choice(indices,500,replace=False))
print [files[i] for i in selected_indices]
However, there is a much better way of doing it. Please see @OlehPrypin's answer
Upvotes: 2
Reputation: 34116
import glob
import random
files = glob.glob('*.jpg')
selected_files = random.sample(files, 500)
print(selected_files)
Both of the methods have a problem when Sample larger than population
. So instead of 500
we can use min(500, len(files))
if needed.
Upvotes: 5