How to incrementally sample without replacement?

前端 未结 13 1624
别跟我提以往
别跟我提以往 2020-12-05 00:38

Python has my_sample = random.sample(range(100), 10) to randomly sample without replacement from [0, 100).

Suppose I have sampled n

13条回答
  •  误落风尘
    2020-12-05 01:19

    It's surprising this is not already implemented in one of the core functions, but here is the clean version, that returns the sampled values and the list without replacement:

    def sample_n_points_without_replacement(n, set_of_points):
        sampled_point_indices = random.sample(range(len(set_of_points)), n)
        sampled_point_indices.sort(reverse=True)
        sampled_points = [set_of_points[sampled_point_index] for sampled_point_index in sampled_point_indices]
        for sampled_point_index in sampled_point_indices:
            del(set_of_points[sampled_point_index])
        return sampled_points, set_of_points
    

提交回复
热议问题