On Jun 23, 4:46 pm, Rick Harrison <n...@[EMAIL PROTECTED]
> wrote:
> By Jove I think I've got it.
>
> initials = ['b', 'c', 'd']
> medials = ['a', 'i', 'u']
> finals = [' ', 'n', 's']
> out_file = open("syllables.txt","w")
> for na in initials:
> for nb in medials:
> for nc in finals:
> x = na + nb + nc
> out_file.write(x+"\n")
> out_file.close()
> print "Finished. It was a pleasure to serve you."
>
> Now to figure out how to randomly shuffle the list before writing it to
> the file.
Python is pretty well known for having all kinds of stuff you want to
do built-in, like random.shuffle:
im****t random
initials = ['b', 'c', 'd']
medials = ['a', 'i', 'u']
finals = [' ', 'n', 's']
syllables = []
for na in initials:
for nb in medials:
for nc in finals:
x = na + nb + nc
syllables.append(x)
random.shuffle(syllables)
out_file = open("syllables.txt","w")
for syllable in syllables:
out_file.write(x+"\n")
out_file.close()
print "Finished. It was a pleasure to serve you."
And if you really want to be slick, and you have the latest beta of
Python (2.6) you can use itertools.product:
im****t itertools, random
syllables = list(itertools.product('bcd','aiu',' ns'))
random.shuffle(syllables)
out_file = open("syllables.txt","w")
for syllable in syllables:
out_file.write(x+"\n")
out_file.close()
Carl Banks


|