[Tutor] 'Name Genereator' Question

Curtis Larsen curtis.larsen@Covance.Com
Wed, 03 Jan 2001 11:44:24 -0600


I'm writing a quickie little script that generates names, to better
understand string manipulation.  What I'd like to have is something like
"map" that can be used against a number of occurrences of a character in
a string and replace it -- without using a lot of loops.  Here's an
example:


# Create names based on a filter where "." equals a random consonant
and "," is a random vowel.
# The first argument is the # of names to generate, the second is the
filter,
# and other letters can be used (including spaces) to fix a certain
character for that spot.
# Example: namegen.py 10 ".,..,." might generate ten names like
"becker", while
#          namegen.py 5 "j,.. r,bb,t" would only create five names like
"jack rabbit".


import random, string, sys

vowels = "aeiou"
cons = "bcdfghjklmnpqrstvwxyz"

name_count = int(sys.argv[1])
name_filter = sys.argv[2]

for names in range(name_count):

    name = name_filter
    
    # The following doesn't work of course (since 'name' is a string),
    # but its close to what I'd LIKE to do:

    for "." in name:
        map(string.replace(name,".",cons[random.randint(0,21)],1))

    for "," in name:
        map(string.replace(name,",",vowels[random.randint(0,5)],1))

    print name



The kicker (to me) is how to call "randint" for each character
replacement target in "name".  The easy way would be to "while" loop for
"'.' in name", etc., or I could create a function to do the replacement
and call it each time, but I thought there would be a more
straightforward answer.  (I'm probably missing something really, really
simple here, right??)

Thanks,
Curtis

PS: I know "randrange" replaces "randint", but I'm not using 2.0 on the
system in question.



-----------------------------------------------------
Confidentiality Notice: This e-mail transmission 
may contain confidential or legally privileged 
information that is intended only for the individual 
or entity named in the e-mail address. If you are not 
the intended recipient, you are hereby notified that 
any disclosure, copying, distribution, or reliance 
upon the contents of this e-mail is strictly prohibited. 

If you have received this e-mail transmission in error, 
please reply to the sender, so that we can arrange 
for proper delivery, and then please delete the message 
from your inbox. Thank you.