Generating a random subsequence

Tom Bryan tbryan at python.net
Fri Jan 4 21:13:20 EST 2002


I needed to get a random subset of a list, and I was surprised that random 
didn't support it directly.  This function was about the best I could do 
given the time constraints.  I post it so that others can criticize and 
improve it.  

def randomSubseq( seq, length ):
        """getRandomSubSeq( seq, length ) -> subsequence of seq of size length
        Generate a (kind of) random subsequence of the specified length and of the 
        same sequence type as the argument seq."""
        import random
        returnSeq = seq[0:0] # get a sequence of the same type as seq   
        try:
                getattr( seq, 'append' )
                mutable = 1
        except AttributeError:
                mutable = 0
        indexes = range( len( seq ) )
        random.shuffle( indexes )
        if mutable:
                for idx in range(length):
                        returnSeq.append( seq[indexes[idx]] )
        else:
                for idx in range(length):
                        returnSeq = returnSeq + seq[indexes[idx]] 
        return returnSeq

Note that I didn't really need *random* sublists.  I was generating some 
test data for another application, and I didn't want to use something like 
seq[:length] because the elements of the list had other properties that 
were usually consistent for short, contiguous subsets.  So, I just needed 
to generate non-contiguous subsequences of varying sizes for a few dozen 
lists of different sizes.

Of course, that made me wonder whether there was a better/more standard way 
to do it.

there's-only-one-way-to-do-it-(plus-or-minus-indentation-style)-ly y'rs
---Tom




More information about the Python-list mailing list