How to iterate on a changing dictionary

Terry Reedy tjreedy at udel.edu
Mon Jun 20 13:37:56 EDT 2011


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




More information about the Python-list mailing list