sample from list python code example
Example 1: python choose random sample from list
import random
sequence = [i for i in range(20)]
subset = random.sample(sequence, 5)
print(subset)
Example 2: python choose sample from list with replacement
import random
random.choices(list, k = 4)
Example 3: python equivalent of R sample function
lstDemand = [0, 1000, 2000, 3000, 4000, 5000, 6000]
lstProbability = [.02, .03, .05, .08, .33, .29, .20]
numpy.random.choice(lstDemand, size=1000, replace=True, p=lstProbability)
Example 4: python choose random sample from list
import random
sequence = [i for i in range(20)]
subset = sample(sequence, 5)
print(subset)