Random sample of paired lists in Python
You can sample m pairs and split those into two lists with the following code:
import random
x = list(range(1, 10))
y = list("abcdefghij")
m = 3
x_sub, y_sub = zip(*random.sample(list(zip(x, y)), m))
If you have two lists of the same dimensions, you just want to sample a subset of these elements and pair the results.
x = [1,2,3,4,5]
y = [6,7,8,9,10]
sample_size = 3
idx = np.random.choice(len(x), size=sample_size, replace=False)
pairs = [(x[n], y[n]) for n in idx]
>>> pairs
[(5, 10), (2, 7), (1, 6)]
If you have two lists with elements that are direct pairs of each other and simply zip
them (and in python 3, cast that object into a list
), then use random.sample
to take a sample.
>>> m = 4
>>> x = list(range(0, 3000, 3))
>>> y = list(range(0, 2000, 2))
>>> random.sample(list(zip(x, y)), m)
[(2145, 1430), (2961, 1974), (9, 6), (1767, 1178)]