Re: [Tutor] newbie code review please!

2008-02-04 Thread tyler
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

Re: [Tutor] newbie code review please!

2008-02-04 Thread Kent Johnson
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 w

Re: [Tutor] newbie code review please!

2008-02-04 Thread Tyler Smith
On Sun, Feb 03, 2008 at 04:35:08PM -0500, Kent Johnson made several helpful suggestions: Thanks! 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. The closest I could get was word_hash[(w1, w2)].pop(), but then I n

Re: [Tutor] newbie code review please!

2008-02-04 Thread tyler
On Sun, Feb 03, 2008 at 04:35:08PM -0500, Kent Johnson made several helpful suggestions: Thanks! 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. The closest I could get was word_hash[(w1, w2)].pop(), but then I n

Re: [Tutor] newbie code review please!

2008-02-03 Thread Kent Johnson
Tyler Smith wrote: > iflist = ifstring.replace('\n', ' ').split() The replace is not needed, split() splits on any whitespace including newlines. > # build hash of form (prefix1, prefix2): [word] > word_hash = {} > for i in range(len(iflist) - 2): > pr1 = iflist[i] > pr2 = iflist[i+1] >

[Tutor] newbie code review please!

2008-02-03 Thread Tyler Smith
Hi, I'm a middling amateur coder in C and elisp, trying to wrap my head around python syntax. I've implemented the code for the markov chain random text generator from Kernighan and Pike's "The practice of programming", and I'd appreciate any tips on how to make it more pythonic. The code appears