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


Groups > comp.lang.python > #40735

Re: iterating over a list as if it were a circular list

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 <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.018
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'empty,': 0.09; 'iterate': 0.09; 'cc:addr:python-list': 0.10; 'assume': 0.11; 'duplicating': 0.16; 'early.': 0.16; 'wrote:': 0.17; 'element': 0.17; 'items.': 0.17; 'email addr:gmail.com&gt;': 0.20; 'keyboard': 0.22; "i'd": 0.22; 'cc:2**0': 0.23; '&gt;': 0.23; 'elements': 0.23; 'seems': 0.23; 'random': 0.24; 'cc:addr:python.org': 0.25; 'header:In- Reply-To:1': 0.25; 'wondering': 0.26; 'am,': 0.27; 'message- id:@mail.gmail.com': 0.27; 'subject:list': 0.28; 'run': 0.28; 'restart': 0.29; 'points': 0.29; 'skip:& 10': 0.29; 'point.': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'but': 0.36; 'wanted': 0.36; 'be.': 0.36; 'too': 0.36; 'far': 0.37; 'subject:: ': 0.38; 'list,': 0.39; 'little': 0.39; 'header:Received:5': 0.40; 'end': 0.40; '2013': 0.84; 'sender:addr:chris': 0.84; 'subject:over': 0.84; 'mean.': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:x-received:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=8XsA5JIpUCKq7ulGL8bgZD1Hou4+71e7ZA49XzPi874=; b=Un4SdTEqcFncR2X7GzFBDOMlaXk1mNl3NdzpFCDc7oBsQwCoAn4CL7RZF0WXgaU94G CzwMMgDRpQpMp4+bz8HAnLyHDnaWNcJwvCRw93rN9qGBX7xZcFcbwWas7ZZu8Xdvs23x Dnht5eaCzhdaDqNhSEMLT8cC3hs9M++ZjvuKE=
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:x-received:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :x-gm-message-state; bh=8XsA5JIpUCKq7ulGL8bgZD1Hou4+71e7ZA49XzPi874=; b=i/m/o23vLgw3TqV0Xm+zW/UIGJamHg578rzrP7MOkg7MfHw7Bo3bXXlk5UF/G22+1U n8bDxUmX7VWS2JHdHftCCBKDQ2rUzPwCRZYIyDorcSqdVX2PN+Xkjz37+Kz8M8BDKRGR Q+cSLZ9Y2VX6sTaGWx4B3dYESHzwLFdixzz5kqRJfdwr5Mx/ufAI/lfeUubYDIMYyHXr rsby8F6YCrMam4jYq2q8Mss+Rg9JKKOteTykll2Km/JLV2ACHpytyVyrdpbwx0Z+23HC e0NeSk3/vDdxBhsF/YMhtnuoYv3q3VMX1C6jU13p+JajNsTXtDsnc3Dg2cnHZT/vWJp9 tEWQ==
MIME-Version 1.0
X-Received by 10.50.88.165 with SMTP id bh5mr13864842igb.71.1362649112868; Thu, 07 Mar 2013 01:38:32 -0800 (PST)
Sender chris@rebertia.com
In-Reply-To <CAEH=cXXf8WVDjUsq2YFvf338+V3hgXChqQaNzpKGGVHf9QrMLQ@mail.gmail.com>
References <CAEH=cXXf8WVDjUsq2YFvf338+V3hgXChqQaNzpKGGVHf9QrMLQ@mail.gmail.com>
Date Thu, 7 Mar 2013 01:38:32 -0800
X-Google-Sender-Auth M5tkn793Thlhxn56AiKkTDCTaxw
Subject Re: iterating over a list as if it were a circular list
From Chris Rebert <clp2@rebertia.com>
To Sven <svenito@gmail.com>
Content-Type multipart/alternative; boundary=e89a8f3ba159539a9404d7527851
X-Gm-Message-State ALoCoQkRI1rsKSPQ6Wv5n+KcJT5TCuZcrjHkiaeRQO8fQ33IFNAI2XqkYk2OxcLR5P1msomjvXR5
Cc Python <python-list@python.org>
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.3000.1362649116.2939.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1362649116 news.xs4all.nl 6923 [2001:888:2000:d::a6]:41154
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:40735

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On Mar 7, 2013 1:29 AM, "Sven" <svenito@gmail.com> wrote:
>
> Stupid keyboard shortcuts, sent it too early. Apologies
>
>
> I was wondering what the best approach for the following might be.
>
> Say you have a list P of points and another list N of other items. You
can always assume that
>
> len(N) <= len(P)
>
> Now I would like to iterate over P and place one N at each point. However
if you run out of N I'd like to restart from N[0] and carry on until all
the points have been populated.
<snip>
> Additionally, what if I wanted to pull a random element from N, but I
want to ensure all elements from N have been used before starting to pick
already chosen random elements again.
> So far I thought of duplicating the list and removing the randomly chosen
elements from the list, and when it's empty, re-copying it. But that seems
a little "wrong" if you know what I mean.

Just iterate over the list in order, and random.shuffle() the list each
time you reach the end of it.

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


Thread

Re: iterating over a list as if it were a circular list Chris Rebert <clp2@rebertia.com> - 2013-03-07 01:38 -0800

csiph-web