What is the difference between random.sample and random.shuffle in Python

后端 未结 6 1733
生来不讨喜
生来不讨喜 2021-01-20 00:23

I have a list a_tot with 1500 elements and I would like to divide this list into two lists in a random way. List a_1 would have 1300 and list a_2 would have 200 elements. My

6条回答
  •  野性不改
    2021-01-20 00:55

    There are two major differences between shuffle() and sample():

    1) Shuffle will alter data in-place, so its input must be a mutable sequence. In contrast, sample produces a new list and its input can be much more varied (tuple, string, xrange, bytearray, set, etc).

    2) Sample lets you potentially do less work (i.e. a partial shuffle).

    It is interesting to show the conceptual relationships between the two by demonstrating that is would have been possible to implement shuffle() in terms of sample():

    def shuffle(p):
       p[:] = sample(p, len(p))
    

    Or vice-versa, implementing sample() in terms of shuffle():

    def sample(p, k):
       p = list(p)
       shuffle(p)
       return p[:k]
    

    Neither of these are as efficient at the real implementation of shuffle() and sample() but it does show their conceptual relationships.

提交回复
热议问题