[Tutor] Indexing in a series for a newbie
dyoo at hkn.eecs.berkeley.edu
Thu Jan 19 19:53:19 CET 2006
On Thu, 19 Jan 2006, Jon Moore wrote:
> I need some help for a program I am writing as a newbie to Python.
> I have created a series:
> WORDS = ("python", "program", "code", "xylophone")
> and then assigned one of them randomly to the variable 'word':
> word = random.choice(WORDS)
> I will get the corresponding word back. But how do I find 'programaticaly'
> the index number for the string that random.choice has chosen?
If you need the index as well as the sequence element, then it may be more
practical to select a random index rather than a random element. The
reason is because it's easy to get a sequence element from its index, but
not necessarily visa-versa.
The random.randrange() function may be useful here.
More information about the Tutor