[Tutor] newbie code review please!

tyler tyler.smith at mail.mcgill.ca
Tue Feb 5 00:53:38 CET 2008


On Mon, Feb 04, 2008 at 03:27:35PM -0400, tyler wrote:
> On Mon, Feb 04, 2008 at 11:56:17AM -0500, Kent Johnson wrote:
> > Tyler Smith wrote:
> >
> >> That cleaned up a lot. However, I couldn't figure out a way to do
> >> random.choice(word_hash[(w1, w2)]) on a dict with set-type values.
> >
> > Ah, sorry; random.choice() needs an indexable sequence. Try
> >   w1, w2 = w2, random.sample(word_hash[(w1, w2)], 1)[0]
> >
> > random.sample() works with any iterable, not just sequences. It returns  
> > a list, hence the indexing [0].
> >

Excellent, thanks!
 
Tyler
 


More information about the Tutor mailing list