Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.076 X-Spam-Evidence: '*H*': 0.85; '*S*': 0.00; 'indices': 0.07; '40,': 0.09; 'iterate': 0.09; 'subject:question': 0.10; 'def': 0.12; '20]': 0.16; '24,': 0.16; 'correlation.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iteration': 0.16; 'iteratively': 0.16; 'nodes': 0.16; 'spurious': 0.16; 'accordingly.': 0.16; 'elements': 0.16; 'index': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'header:In-Reply-To:1': 0.27; 'fixed': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'piece': 0.31; 'produces': 0.31; 'provided,': 0.31; 'fri,': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'introducing': 0.36; 'picking': 0.36; 'sequence': 0.36; 'subject:Simple': 0.36; 'possible': 0.36; 'list': 0.37; 'easiest': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'highest': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'remove': 0.60; 'most': 0.60; 'tell': 0.60; 'simple': 0.61; 'visit': 0.64; 'more': 0.64; 'different': 0.65; 'taking': 0.65; '30,': 0.65; '20,': 0.68; 'risk': 0.72; 'obvious': 0.74; 'special': 0.74; '(ie': 0.84; 'idiot': 0.84; 'simulation': 0.91; 'imagine': 0.93; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=UDZQ1vPHGE9Jt42WB7qFRk8y+KzXixUbNITdgyey4to=; b=P8dNorTAssyOYKofCOtXl+6kOzkoO0xq/MqzGZeEJ316TqtCwnlsEg4MaSEi8zP47k TWFFDbF3LwXlBDhvuM5rxRZzyDDbPT2n/GZpnn+iujN3q8Ha590VjB2p8V4+6vMaqX+E ukVUHg+YoYzK6uNsop5K+RHuQ1j+VjY/ic99xQEvC4I/0RguN6dLGq1GNqycNleWldIG zz4HATf1UnI4Necmy/0CZ21wz8C2133K/Wwvk9xXD86+KcjzFHjE8IkuJ8eXdd6pBLmu /dCPXXJJEPKGhwObnOd+LCbogdX7en4w+E56a0GE41maUHTPD3zUxTaJ+L/j8kmbbKX6 ueCQ== MIME-Version: 1.0 X-Received: by 10.220.167.69 with SMTP id p5mr7742088vcy.57.1369384669390; Fri, 24 May 2013 01:37:49 -0700 (PDT) In-Reply-To: References: Date: Fri, 24 May 2013 18:37:49 +1000 Subject: Re: Simple algorithm question - how to reorder a sequence economically From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 66 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1369384673 news.xs4all.nl 15938 [2001:888:2000:d::a6]:51087 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45871 On Fri, May 24, 2013 at 6:14 PM, Peter Brooks wrote: > 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. Permuting a sequence iteratively to cover every possibility? Good fun. Here's one way of looking at it. Imagine the indices of all elements are in some special "base" like so: [a, b, c, d] --> a*4+b*3+c*2+d*1 Then iterate up to the highest possible value (ie 4*3*2*1), picking indices for each accordingly. I don't know how efficient this will be, but here's a simple piece of code to do it: >>> def permute(lst,pos): lst=lst[:] # Take a copy ret=[None]*len(lst) for i in range(len(lst)): pos,idx=divmod(pos,len(lst)) ret[i]=lst[idx] del lst[idx] return ret >>> for i in range(4*3*2*1): permute([10,20,30,40],i) [10, 20, 30, 40] [20, 10, 30, 40] [30, 10, 20, 40] [40, 10, 20, 30] [10, 30, 20, 40] [20, 30, 10, 40] [30, 20, 10, 40] [40, 20, 10, 30] [10, 40, 20, 30] [20, 40, 10, 30] [30, 40, 10, 20] [40, 30, 10, 20] [10, 20, 40, 30] [20, 10, 40, 30] [30, 10, 40, 20] [40, 10, 30, 20] [10, 30, 40, 20] [20, 30, 40, 10] [30, 20, 40, 10] [40, 20, 30, 10] [10, 40, 30, 20] [20, 40, 30, 10] [30, 40, 20, 10] [40, 30, 20, 10] It works, it produces a unique list for any given index provided, but it's not the cleanest or most efficient. But I know someone'll improve on it... or tell me I'm an idiot for not taking a more obvious approach :) ChrisA