Get random sample from list while maintaining ordering of items?

后端 未结 5 2101
清歌不尽
清歌不尽 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:04

    Maybe you can just generate the sample of indices and then collect the items from your list.

    randIndex = random.sample(range(len(mylist)), sample_size)
    randIndex.sort()
    rand = [mylist[i] for i in randIndex]
    

提交回复
热议问题