random from an array
Peter Hansen
peter at engcorp.com
Fri Aug 6 18:35:01 EDT 2004
Calvin79 wrote:
> I've just started to try python so forgive the ignorance!
>
> I've been using this, or something like it;
>
> First = random.choice ('abc')
This works not because choice() takes a string, but because
even though it takes a sequence, a string *is* a sequence
(of bytes).
> What I want to do, however, is take a random pick from something like the
> following;
>
> First = random.??? ("rna", "rnb", "rnc") - this of course "random.choice"
> doesn't work.
Just use another sequence, either a tuple or a list:
random.choice(("rna", "rnb", "rnc"))
or
random.choice(["rna", "rnb", "rnc"])
The latter is preferred for various mostly stylistic reasons...
-Peter
More information about the Python-list
mailing list