[Tutor] Python incrementing problem
eryksun
eryksun at gmail.com
Wed Oct 17 13:12:04 CEST 2012
On Thu, Oct 11, 2012 at 3:29 PM, Stephen Hooker <sctjkh at googlemail.com> wrote:
>
> I try to store 1 result it works fine. If I try to store more than 1 result
> I get one or the other, never both. The code is below (not the whole
> program)
>
> for i in range(2):
> pos = random.randrange(0,len(rolling_stock_OS))
> pick = rolling_stock_OS[pos]
> for item in noDupes:
> if item==pick:
> break
> else: # else for the loop, executed if the loop ran to exhaustion
> noDupes.append(pick)
The formatting was lost, but it seems you're trying to use two for
loops to do something that's easier with a single while loop, and even
easier if you take a random.sample() of a set():
http://docs.python.org/release/2.6.7/library/random.html#random.sample
>>> import random
>>> data = [3,1,4,5,9,3,1,4,5,9]
>>> random.sample(set(data), 5)
[9, 3, 5, 4, 1]
More information about the Tutor
mailing list