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


Groups > comp.lang.python > #8020

Re: How to iterate on a changing dictionary

Path csiph.com!x330-a1.tempe.blueboxinc.net!feeder3.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'elements.': 0.07; 'subject:changing': 0.07; 'terry': 0.07; 'accidentally': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'am,': 0.14; 'wrote:': 0.14; 'container,': 0.16; 'immutable': 0.16; 'reedy': 0.16; 'remove.': 0.16; 'jan': 0.20; 'header:In-Reply-To:1': 0.21; 'seems': 0.21; 'modify': 0.22; 'repeatedly': 0.22; 'structure': 0.23; 'random': 0.28; 'subject:How': 0.30; 'header:X-Complaints-To:1': 0.32; 'to:addr:python-list': 0.33; 'list': 0.33; 'header:User-Agent:1': 0.35; 'element': 0.37; 'sequence': 0.37; 'solutions.': 0.37; 'received:org': 0.38; 'subject:: ': 0.38; 'should': 0.39; 'header :Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'needed.': 0.40; 'unique': 0.63; 'play': 0.64; 'collection': 0.72; 'game,': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: How to iterate on a changing dictionary
Date Mon, 20 Jun 2011 13:37:56 -0400
References <itl1a5$rom$1@speranza.aioe.org> <4dfe2b95$1@dnews.tpgi.com.au> <itnhfp$ab0$1@speranza.aioe.org> <BANLkTinGc7gr0iCdOvtm9KP4yqSrO=5Dow@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host rain.gmane.org
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10
In-Reply-To <BANLkTinGc7gr0iCdOvtm9KP4yqSrO=5Dow@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
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.184.1308591490.1164.python-list@python.org> (permalink)
Lines 23
NNTP-Posting-Host 82.94.164.166
X-Trace 1308591491 news.xs4all.nl 49038 [::ffff:82.94.164.166]:40604
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:8020

Show key headers only | View raw


On 6/20/2011 10:30 AM, Florencio Cano wrote:
>> To make an example: imaging Bingo.Shuffle the numbers, each number sorted
>> should be removed from the container, how would it implemented?
>
> The structure seems a set ->  unordered collection of unique elements.
>
> You can select a random element from the set with
> random.sample(container, num_of_elems)
>
> And then remove the elem from the set with remove.

random.sample(someset) just turns the set into a sequence (tuple).
For bingo, where you will play multiple games,
start with an immutable collection balls = ('A1', 'A2', ...)
that you cannot accidentally modify and can repeatedly copy.
For each game, copy to a list game = list(balls).
Then random.shuffle(game), and game.pop() balls as needed.

Other situations will need other solutions.

-- 
Terry Jan Reedy

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


Thread

How to iterate on a changing dictionary TheSaint <nobody@nowhere.net.no> - 2011-06-19 22:32 +0800
  Re: How to iterate on a changing dictionary Chris Angelico <rosuav@gmail.com> - 2011-06-20 01:13 +1000
    Re: How to iterate on a changing dictionary Roy Smith <roy@panix.com> - 2011-06-19 11:53 -0400
      Re: How to iterate on a changing dictionary Terry Reedy <tjreedy@udel.edu> - 2011-06-19 12:51 -0400
  Re: How to iterate on a changing dictionary Terry Reedy <tjreedy@udel.edu> - 2011-06-19 12:02 -0400
  Re: How to iterate on a changing dictionary Lie Ryan <lie.1296@gmail.com> - 2011-06-20 03:00 +1000
    Re: How to iterate on a changing dictionary TheSaint <nobody@nowhere.net.no> - 2011-06-20 21:20 +0800
      Re: How to iterate on a changing dictionary Florencio Cano <florencio.cano@gmail.com> - 2011-06-20 16:30 +0200
      Re: How to iterate on a changing dictionary Terry Reedy <tjreedy@udel.edu> - 2011-06-20 13:37 -0400
        Re: How to iterate on a changing dictionary TheSaint <nobody@nowhere.net.no> - 2011-06-21 18:44 +0800

csiph-web