Re: shuffling elements of a list

2006-06-02 Thread Peter Otten
Iain King wrote: > or shorter but possible less readable (and only in 2.4+): > > def shuffle(data): > return [y[1] for y in sorted([(random(), x) for x in data])] sorted() and list.sort() will happily accept a key function argument and then do the decorating/undecorating for you: >>> from r

Re: shuffling elements of a list

2006-06-02 Thread Iain King
David C. Ullrich wrote: > On 30 May 2006 21:53:32 -0700, "greenflame" <[EMAIL PROTECTED]> > wrote: > > That's DSU for _sorting_ a list. I read about this, thought > it was pretty neat. I thought that the fact that you > could use the same trick for _shuffling_ a list was > my idea, gonna make me r

Re: shuffling elements of a list

2006-06-02 Thread David C.Ullrich
On Thu, 01 Jun 2006 03:25:23 -0700, Erik Max Francis <[EMAIL PROTECTED]> wrote: >David C. Ullrich wrote: > >> Good example, because we know that EMF is not dumb. I've seen >> the same algorithm many times - the best example is ... > >Man, an error made _six years ago_ and people are still bringing

Re: shuffling elements of a list

2006-06-01 Thread Alex Martelli
Peter Otten <[EMAIL PROTECTED]> wrote: > Gerard Flanagan wrote: > > 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 ;-) >

Re: shuffling elements of a list

2006-06-01 Thread Gerard Flanagan
Peter Otten wrote: > Gerard Flanagan wrote: > > > 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 ;-) > > > > pi

Re: shuffling elements of a list

2006-06-01 Thread Erik Max Francis
David C. Ullrich wrote: > Good example, because we know that EMF is not dumb. I've seen > the same algorithm many times - the best example is ... Man, an error made _six years ago_ and people are still bringing it up ... -- Erik Max Francis && [EMAIL PROTECTED] && http://www.alcyone.com/max/ Sa

Re: shuffling elements of a list

2006-06-01 Thread David C.Ullrich
On Wed, 31 May 2006 23:05:14 +0200, Fredrik Lundh <[EMAIL PROTECTED]> wrote: >Roger Miller wrote: > >> DSU seems like a lot of trouble to go through in order to use an O(n >> log n) sorting algorithm to do what can be done in O(N) with a few >> lines of code. The core code of random.shuffle() sho

Re: shuffling elements of a list

2006-06-01 Thread Peter Otten
Gerard Flanagan wrote: > 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) ] >

Re: shuffling elements of a list

2006-06-01 Thread Ben Finney
"Gerard Flanagan" <[EMAIL PROTECTED]> 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()

Re: shuffling elements of a list

2006-06-01 Thread Gerard Flanagan
Ben Finney wrote: [snip] > > Please don't write C in Python. The 'for' statement allows iteration > directly over a sequence, no need to maintain an index. Also, the > modulus operator is called for with your cycling of the pile index. > > pile_index = 0 > for card in deck: >

Re: shuffling elements of a list

2006-05-31 Thread Fredrik Lundh
Roger Miller wrote: > DSU seems like a lot of trouble to go through in order to use an O(n > log n) sorting algorithm to do what can be done in O(N) with a few > lines of code. The core code of random.shuffle() shows how easy it is > to do it right: > > for i in reversed(xrange(1, len(x)

Re: shuffling elements of a list

2006-05-31 Thread Roger Miller
Sybren Stuvel wrote: > David C Ullrich enlightened us with: > > I thought that the fact that you could use the same trick for > > _shuffling_ a list was my idea, gonna make me rich and famous. I > > guess I'm not the only one who thought of it. Anyway, you can use > > DSU to _shuffle_ a list by de

Re: shuffling elements of a list

2006-05-31 Thread David C.Ullrich
On Wed, 31 May 2006 12:17:11 +0200, Sybren Stuvel <[EMAIL PROTECTED]> wrote: >David C Ullrich enlightened us with: >> I thought that the fact that you could use the same trick for >> _shuffling_ a list was my idea, gonna make me rich and famous. I >> guess I'm not the only one who thought of it.

Re: shuffling elements of a list

2006-05-31 Thread Sybren Stuvel
David C Ullrich enlightened us with: > I thought that the fact that you could use the same trick for > _shuffling_ a list was my idea, gonna make me rich and famous. I > guess I'm not the only one who thought of it. Anyway, you can use > DSU to _shuffle_ a list by decorating the list with random n

Re: shuffling elements of a list

2006-05-31 Thread SuperHik
greenflame wrote: > Zhang Fan wrote: >> On 30 May 2006 20:18:19 -0700, greenflame <[EMAIL PROTECTED]> wrote: >>> 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. >> The random module has

Re: shuffling elements of a list

2006-05-31 Thread David C.Ullrich
On 30 May 2006 21:53:32 -0700, "greenflame" <[EMAIL PROTECTED]> wrote: >Thank you all for all of your help. Also I got the shuffle function to >work. Do not worry I will be back soon with more shuffling! However, I >do not quite understand this DSU that you mention, although it looks >useful. I d

Re: shuffling elements of a list

2006-05-30 Thread greenflame
Thank you all for all of your help. Also I got the shuffle function to work. Do not worry I will be back soon with more shuffling! However, I do not quite understand this DSU that you mention, although it looks useful. -- http://mail.python.org/mailman/listinfo/python-list

Re: shuffling elements of a list

2006-05-30 Thread Scott David Daniels
greenflame wrote: > Zhang Fan wrote: >> ... The random module has a `shuffle' method. It "Shuffle the sequence x >> in place". It may be help for you > > I am sorry but this does not help much. In my version of python (2.3) > this method does not seem to exist. Also from the documentation, it >

Re: shuffling elements of a list

2006-05-30 Thread John Machin
On 31/05/2006 1:18 PM, greenflame wrote: > 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. > In general, if you can't see why Pytho

Re: shuffling elements of a list

2006-05-30 Thread Ben Finney
"greenflame" <[EMAIL PROTECTED]> writes: > 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. Sounds like a job for (ta-da-daa) DSU[0]. That said, here are some comments on your implementation. > -- >

Re: shuffling elements of a list

2006-05-30 Thread greenflame
Zhang Fan wrote: > On 30 May 2006 20:18:19 -0700, greenflame <[EMAIL PROTECTED]> wrote: > > 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. > > The random module has a `shuffle' method.

Re: shuffling elements of a list

2006-05-30 Thread Zhang Fan
On 30 May 2006 20:18:19 -0700, greenflame <[EMAIL PROTECTED]> wrote: > 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. The random module has a `shuffle' method. It "Shuffle the sequence

shuffling elements of a list

2006-05-30 Thread greenflame
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