shuffling elements of a list
Ben Finney
bignose+hates-spam at benfinney.id.au
Thu Jun 1 03:52:00 EDT 2006
"Gerard Flanagan" <grflanagan at yahoo.co.uk> writes:
> Ben Finney wrote:
> > pile_index = 0
> > for card in deck:
> > piles[pile_index].append(card)
> > pile_index = (pile_index + 1) % numpiles
> >
>
> no need to maintain an index ;-)
>
> piles = [ list() for _ in range(n) ]
> for i, card in enumerate(deck):
> piles[i % numpiles].append(card)
That's a matter of style. I prefer what I wrote, since I've given an
explicit name to the calculation you're doing inside the [] operator;
that way, anyone reading the code knows *why* the calculation is done
in this particular case.
If, of course, the index was a simple increment-by-one each time, your
'enumerate' usage would be clearer.
--
\ "We spend the first twelve months of our children's lives |
`\ teaching them to walk and talk and the next twelve years |
_o__) telling them to sit down and shut up." -- Phyllis Diller |
Ben Finney
More information about the Python-list
mailing list