Get random sample from list while maintaining ordering of items?

后端 未结 5 2088
清歌不尽
清歌不尽 2020-12-12 12:44

I have a sorted list, let say: (its not really just numbers, its a list of objects that are sorted with a complicated time consuming algorithm)

mylist = [ 1          


        
5条回答
  •  粉色の甜心
    2020-12-12 13:09

    random.sample implement it.

    >>> random.sample([1, 2, 3, 4, 5],  3)   # Three samples without replacement
    [4, 1, 5]
    

提交回复
热议问题