从列表中获取随机样品,同时保持项目的顺序?

我有一个排序列表,比如说: (它不仅仅是数字,它是一个对象列表,这些对象是用一个复杂的耗时算法排序的)

mylist = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ,9  , 10 ]

有没有一些 python 函数会给我 N 个条目,但是会保持顺序?

例如:

randomList = getRandom(mylist,4)
# randomList = [ 3 , 6 ,7 , 9 ]
randomList = getRandom(mylist,4)
# randomList = [ 1 , 2 , 4 , 8 ]

等等。

115108 次浏览

Following code will generate a random sample of size 4:

import random


sample_size = 4
sorted_sample = [
mylist[i] for i in sorted(random.sample(range(len(mylist)), sample_size))
]

(note: with Python 2, better use xrange instead of range)

Explanation

random.sample(range(len(mylist)), sample_size)

generates a random sample of the indices of the original list.

These indices then get sorted to preserve the ordering of elements in the original list.

Finally, the list comprehension pulls out the actual elements from the original list, given the sampled indices.

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]

Simple-to-code O(#picks*log(#picks)) way

Take a random sample without replacement of the indices, sort the indices, and take them from the original.

indices = random.sample(range(len(myList)), K)
[myList[i] for i in sorted(indices)]

random.sample(seq, K) will randomly and simultaneously pick K elements out of a population in seq, without replacement. When we do this with a range this is O(1) per sample since the range object in python is sparse and doesn't actually construct a full list (specifically the cpython implementation calls len(seq) and later seq[i]'s on the range object, which are virtualized/faked and thus O(1)). Then you look up the random indices (in order).

If you have an iterator (e.g. generator expression), you may consider first converting to a list and then doing the above answer. If your iterator is of unbounded length, you may use the technique in the next section, which is much less performant, but may be intellectually interesting (e.g. if you are working with small bounded lists in a functional language that doesn't yet support indexing, or giant streams that exceed RAM and disk size):

(Also a useful note from user tegan in the comments: If this is python2, you will want to use xrange, as usual. Otherwise you will have an O(N) rather than an O(#picks) algorithm.)


Slow/streamable O(arrayLen)-time, O(1)-auxiliary-space way for non-indexable sequences

You can alternatively use a math trick and iteratively go through myList from left to right, picking numbers with dynamically-changing probability (N-numbersPicked)/(total-numbersVisited). This approach is O(N) since it visits everything once (faster than sorting which is O(N log(N)), though much slower that directly indexing the K picks like we did in the previous section (which was O(K log(K)) after sorting).

from __future__ import division


def orderedSampleWithoutReplacement(seq, k):
if not 0<=k<=len(seq):
raise ValueError('Required that 0 <= sample_size <= population_size')


numbersPicked = 0
for i,number in enumerate(seq):
prob = (k-numbersPicked)/(len(seq)-i)
if random.random() < prob:
yield number
numbersPicked += 1

Proof: Considering the uniform distribution (without replacement) of picking a subset of k out of a population seq of size len(seq), we can consider a partition at an arbitrary point i into 'left' (0,1,...,i-1) and 'right' (i,i+1,...,len(seq)). Given that we picked numbersPicked from the left known subset, the remaining must come from the same uniform distribution on the right unknown subset, though the parameters are now different. In particular, the probability that seq[i] contains a chosen element is #remainingToChoose/#remainingToChooseFrom, or (k-numbersPicked)/(len(seq)-i), so we simulate that and recurse on the result. (This must terminate since if #remainingToChoose == #remainingToChooseFrom, then all remaining probabilities are 1.) This is similar to a probability tree that happens to be dynamically generated. Basically you can simulate a uniform probability distribution by conditioning on prior choices (as you grow the probability tree, you pick the probability of the current branch such that it is aposteriori the same as prior leaves, i.e. conditioned on prior choices; this will work because this probability is uniformly exactly N/k).

(One may look at the edit history of this post to find an elaborate simulation 'proof', which was previously necessary due to some downvotes.)

Here's another way to code it below, with more semantically-named variables.

from __future__ import division
import random


def orderedSampleWithoutReplacement(seq, sampleSize):
totalElems = len(seq)
if not 0<=sampleSize<=totalElems:
raise ValueError('Required that 0 <= sample_size <= population_size')
    

picksRemaining = sampleSize
for elemsSeen,element in enumerate(seq):
elemsRemaining = totalElems - elemsSeen
prob = picksRemaining/elemsRemaining
if random.random() < prob:
yield element
picksRemaining -= 1


from collections import Counter
Counter(
tuple(orderedSampleWithoutReplacement([0,1,2,3], 2))
for _ in range(10**5)
)

edit: Timothy Shields mentions Reservoir Sampling, which is sort of like this method (but starts with candidate picks and swaps them out randomly), and useful when len(seq) is unknown (such as with a generator expression). Specifically the one noted as "algorithm R" is O(N) and O(1) aux space if done in-place; it involves taking the first K elements and slowly replacing them (a hint at an inductive proof is also given). There are also useful variants of reservoir sampling to be found on the wikipedia page. The idea is that you prepopulate a list of candidate return values (which we assume fits in RAM or on disk), and probabilistically swap them out as you iterate through the list (which may be arbitrarily bigger than your RAM or disk).


Optimal O(#picks * (1+log(N/#picks)))-time, O(1)-aux-space way for indexable sequences

One algorithm of note is in the Reservoir Sampling article (ctrl-F Algorithm L section "An optimal algorithm"): it is a competitive-factor optimal algorithm which is (like the original solution) O(k) in the number of samples, not O(n) in the number of elements of the list.

The intuition here is that we can skip arbitrary sections of list without having to even visit them, because the number of elements between picks is not dependent on the data we see in the list.

Instead of relying on the hypergeometric distribution as above, the fact that the reservoir is pre-populated with candidate solutions (the first k items) and periodically swapped-out, makes it apparently act more like a process with geometric waiting-time. It is a well-cited paper, but I cannot access it to tell if the proof is asymptotically correct for large N, or works for all N.

It is unclear from the article if this algorithm can be used when the length of the sequence is not known at start time (in which case one could presumably just use the original method in the first section of this answer).

Apparently random.sample was introduced in python 2.3

so for version under that, we can use shuffle (example for 4 items):

myRange =  range(0,len(mylist))
shuffle(myRange)
coupons = [ bestCoupons[i] for i in sorted(myRange[:4]) ]

random.sample implement it.

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