0

If you have a list of 100 values, which you want to subset into 3 in the ratio 2:1:1, what's the easiest way to do this in Python?

My current solution is to take a sample of the indices for each subset then remove these values from the original list, i.e.

my_list = [....]
num_A = 50
subset_A = []

num_B = 25
subset_B = []

num_C = 25
subset_C = []

a_indices = random.sample(xrange(len(my_list)), num_A)
for i in sorted(a_indices, reverse=True):  # Otherwise can get index out of range
    subset_A.append(my_list.pop(i))

b_indices = random.sample(xrange(len(my_list)), num_B)
for i in sorted(b_indices, reverse=True):  # Otherwise can get index out of range
    subset_B.append(my_list.pop(i))

subset_C = my_list[:]
assert len(subset_C) == num_C

However I'm sure there's a much more elegant solution than this.

Stuart Lacy
  • 1,963
  • 2
  • 18
  • 30

1 Answers1

5

There's a much easier way. You can just shuffle the array and take parts.

xs = [...]
random.shuffle(xs)
print(xs[:50], xs[50:75], xs[75:])
simonzack
  • 19,729
  • 13
  • 73
  • 118