shuffling elements of a list

greenflame alikakakhel at yahoo.com
Tue May 30 23:18:19 EDT 2006


I would like to make a function that takes a list, more specificaly a
list of strings, and shuffles its elements, like a pile of cards. The
following is a script I tryed to make that implements pile shuffling.

----------
testdeck = list('qwertyuiop')

def pileshuffle(DECK, NUMPILES):
    """Split the deck given into NUMPILES piles. Then also put the
piles""" \
             """ together to make the deck again."""

    # Define a list of lists which is the piles
    PILES = [[]] * NUMPILES

    card = 0
    pilenum = 0
    while card < len(DECK):
        PILES[pilenum].append(DECK[card])
        card += 1
        if pilenum < NUMPILES:
            pilenum += 1
        else:
            pilenum = 0

    print PILES
----------

First of all, this script tells me that an index is out of range. I
cannot see why this would be so. Second of all, I would like to have
other methods of shuffling, prefererably riffle shuffling and just
plain randomly arranging the elements of the list.

I very much appreciate any help. Thank you.




More information about the Python-list mailing list