Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45897

RE: Simple algorithm question - how to reorder a sequence economically

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <carlosnepomuceno@outlook.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'algorithm': 0.04; '-for': 0.05; 'cpython': 0.05; '[0,': 0.09; '[1,': 0.09; 'iterate': 0.09; 'snippet': 0.09; 'subject:question': 0.10; 'def': 0.12; 'random': 0.14; '(0,': 0.16; '(3,': 0.16; '0],': 0.16; '2],': 0.16; '3],': 0.16; '[2,': 0.16; 'correlation.': 0.16; 'doing:': 0.16; 'iteration': 0.16; 'itertools': 0.16; 'nodes': 0.16; 'spurious': 0.16; 'subject: \n ': 0.16; 'tuple.': 0.16; 'elements': 0.16; '>>>': 0.22; 'import': 0.22; 'to:name:python-list@python.org': 0.22; 'print': 0.22; 'creating': 0.23; 'mathematical': 0.24; 'received:65.55.116': 0.24; 'looks': 0.24; 'question': 0.24; 'header:In-Reply-To:1': 0.27; 'fixed': 0.29; 'skip:- 40': 0.29; "i'm": 0.30; 'url:mailman': 0.30; 'that.': 0.31; '(on': 0.31; 'url:python': 0.33; 'fri,': 0.33; 'date:': 0.34; 'test': 0.35; 'but': 0.35; 'introducing': 0.36; 'sequence': 0.36; 'subject:Simple': 0.36; 'url:listinfo': 0.36; "didn't": 0.36; 'url:org': 0.36; 'list': 0.37; 'email addr:python.org': 0.37; 'list.': 0.37; 'easiest': 0.38; 'to:addr:python-list': 0.38; 'subject:': 0.39; '\xa0\xa0\xa0': 0.39; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'how': 0.40; 'remove': 0.60; 'is.': 0.60; 'mentioned': 0.61; 'took': 0.61; 'simple': 0.61; 'email addr:gmail.com': 0.63; 'visit': 0.64; 'more': 0.64; 'different': 0.65; 'email name:python-list': 0.65; 'charset:windows-1252': 0.65; 'risk': 0.72; 'afford': 0.91; 'lists:': 0.91; 'simulation': 0.91; '2013': 0.98
X-TMN [zH/cJZ4YlsIJw08tnHw13iqMgXBE3Nvu]
X-Originating-Email [carlosnepomuceno@outlook.com]
From Carlos Nepomuceno <carlosnepomuceno@outlook.com>
To "python-list@python.org" <python-list@python.org>
Subject RE: Simple algorithm question - how to reorder a sequence economically
Date Fri, 24 May 2013 18:00:59 +0300
Importance Normal
In-Reply-To <a31c773f-88d8-4489-8640-12123d884e4d@m2g2000vbb.googlegroups.com>
References <a31c773f-88d8-4489-8640-12123d884e4d@m2g2000vbb.googlegroups.com>
Content-Type text/plain; charset="Windows-1252"
Content-Transfer-Encoding quoted-printable
MIME-Version 1.0
X-OriginalArrivalTime 24 May 2013 15:00:59.0568 (UTC) FILETIME=[8041B300:01CE588F]
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2068.1369407666.3114.python-list@python.org> (permalink)
Lines 76
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1369407666 news.xs4all.nl 15864 [2001:888:2000:d::a6]:57441
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:45897

Show key headers only | View raw


----------------------------------------
> Date: Fri, 24 May 2013 01:14:45 -0700
> Subject: Simple algorithm question - how to reorder a sequence economically
> From: peter.h.m.brooks@gmail.com
> To: python-list@python.org
>
> What is the easiest way to reorder a sequence pseudo-randomly?
>
> That is, for a sequence 1,2,3,4 to produce an arbitrary ordering (eg
> 2,1,4,3) that is different each time.
>
> I'm writing a simulation and would like to visit all the nodes in a
> different order at each iteration of the simulation to remove the risk
> of a fixed order introducing spurious evidence of correlation.
> --
> http://mail.python.org/mailman/listinfo/python-list

I don't know what "spurious evidence of correlation" is. Can you give a mathematical definition?

Here's a snippet for creating a random shuffle by Fisher–Yates algorithm:

def FY_shuffle(l):
    from random import randint
    for i in range(len(l)-1,0,-1):
        j = randint(0,i)
        l[j],l[i] = l[i],l[j]

It looks just like random.shuffle() mentioned before, but you can change it as you see fit.

If you can afford to test all permutations you can iterate over it by doing:

>>> from itertools import permutations
>>> l=range(4)
>>> l
[0, 1, 2, 3]
>>> for i in permutations(l): print i
...
(0, 1, 2, 3)
(0, 1, 3, 2)
(0, 2, 1, 3)
[...]
(3, 1, 2, 0)
(3, 2, 0, 1)
(3, 2, 1, 0)
>>>

Note that 'i' is a tuple.

If you need a list of all permutations to make a selection:

>>> l=range(4)
>>> l
[0, 1, 2, 3]
>>> [list(i) for i in permutations(l)]
[[0, 1, 2, 3], [0, 1, 3, 2], [0, 2, 1, 3], [0, 2, 3, 1], [0, 3, 1, 2], [0, 3, 2, 1], [1, 0, 2, 3], [1, 0, 3, 2], [1, 2, 0, 3], [1, 2, 3, 0], [1, 3, 0, 2], [1, 3, 2, 0], [2, 0, 1, 3], [2, 0, 3, 1], [2, 1, 0, 3], [2, 1, 3, 0], [2, 3, 0, 1], [2, 3, 1, 0], [3, 0, 1, 2], [3, 0, 2, 1], [3, 1, 0, 2], [3, 1, 2, 0], [3, 2, 0, 1], [3, 2, 1, 0]]

This will produce big lists:
-for 10 elements (l=range(10)) the size of the list is about 30MB (on Windows).
-for 11 elements (l=range(11)) the size of the list is about 335MB (on Windows). It took more than 7GB for CPython 2.7.5 to create that list.

Didn't try after that. 		 	   		  

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Simple algorithm question - how to reorder a sequence economically Peter Brooks <peter.h.m.brooks@gmail.com> - 2013-05-24 01:14 -0700
  Re: Simple algorithm question - how to reorder a sequence economically Chris Angelico <rosuav@gmail.com> - 2013-05-24 18:37 +1000
  Re: Simple algorithm question - how to reorder a sequence economically Fábio Santos <fabiosantosart@gmail.com> - 2013-05-24 09:47 +0100
  Re: Simple algorithm question - how to reorder a sequence economically Chris Angelico <rosuav@gmail.com> - 2013-05-24 19:11 +1000
    Re: Simple algorithm question - how to reorder a sequence economically duncan smith <buzzard@invalid.invalid> - 2013-05-24 15:33 +0100
  Re: Simple algorithm question - how to reorder a sequence economically Terry Jan Reedy <tjreedy@udel.edu> - 2013-05-24 06:10 -0400
  Re: Simple algorithm question - how to reorder a sequence economically Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-24 10:52 +0000
    Re: Simple algorithm question - how to reorder a sequence economically Ned Batchelder <ned@nedbatchelder.com> - 2013-05-24 07:26 -0400
      Re: Simple algorithm question - how to reorder a sequence economically Peter Brooks <peter.h.m.brooks@gmail.com> - 2013-05-24 06:23 -0700
        Re: Simple algorithm question - how to reorder a sequence economically Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-24 13:57 +0000
        Re: Simple algorithm question - how to reorder a sequence economically Robert Kern <robert.kern@gmail.com> - 2013-06-12 14:14 +0100
    Re: Simple algorithm question - how to reorder a sequence economically John Ladasky <john_ladasky@sbcglobal.net> - 2013-05-24 10:33 -0700
      Re: Simple algorithm question - how to reorder a sequence economically John Ladasky <john_ladasky@sbcglobal.net> - 2013-05-25 18:25 -0700
        Re: Simple algorithm question - how to reorder a sequence economically Roy Smith <roy@panix.com> - 2013-05-25 21:49 -0400
  RE: Simple algorithm question - how to reorder a sequence economically Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-24 18:00 +0300
    Re: Simple algorithm question - how to reorder a sequence economically Peter Brooks <peter.h.m.brooks@gmail.com> - 2013-05-24 12:01 -0700
      RE: Simple algorithm question - how to reorder a sequence economically Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-25 00:33 +0300
        Re: Simple algorithm question - how to reorder a sequence economically Peter Brooks <peter.h.m.brooks@gmail.com> - 2013-05-24 17:28 -0700

csiph-web