Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Education > Language Artificial > Re: Python prog...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 4 Topic 920 of 953
Post > Topic >>

Re: Python programming question

by Carl Banks <pavlovevidence@[EMAIL PROTECTED] > Jun 23, 2008 at 06:38 PM

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
 




 4 Posts in Topic:
Python programming question
Rick Harrison <not@[EM  2008-06-22 23:17:53 
Re: Python programming question
Rick Harrison <not@[EM  2008-06-23 16:46:33 
Re: Python programming question
Carl Banks <pavlovevid  2008-06-23 18:38:47 
Re: Python programming question
Rick Harrison <_nickna  2008-06-25 03:53:04 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Nov 22 4:21:57 CST 2008.