[Tutor] Re: (no subject)

Lee Harr missive at hotmail.com
Tue Dec 23 23:20:02 EST 2003


>I have a question concerning turning strings into variables.
>
>Say I have a list like x:
>
>x = ['hello', 'bye']
>
>What I need is the final result to look like this, or the equivelant of
>this. How would I do this???
>
>hello = re.compile(r'<!--INSERT HELLO HERE-->')
>bye = re.compile(r'<!--INSERT BYE HERE-->')
>
>I think it would look something like
>
>for y in x:
>   #string to variable# = re.compile(r'<--INSERT %s HERE-->' % y)
>


I think, generally, you don't really want to do that. How about creating
a dictionary instead? Or another list?


my_regexps = {}
for find_this in x:
    my_regexps[find_this] = re.compile(r'<--INSERT %s HERE-->' % find_this)


>Is this the most effecient way, and how would I convert y to capitols.
>

>>>dir('hello')
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', 
'__ge__', '__getattribute__', '__getitem__', '__getnewargs__', 
'__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', 
'__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', 
'__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 
'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 
'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 
'replace', 'rfind', 'rindex', 'rjust', 'rstrip', 'split', 'splitlines', 
'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>>'hello'.upper()
'HELLO'




>Also can someone explain lambdas too me. Whenever I do something like:
>
>x = lambda x: x + 1, (1)
>
>I get (<function whatever blah>, 2)


I think what you want is

x = lambda x: x + 1
x(1)

what you have is a tuple of a lambda and a number.

Also, I think if you are assigning the lambda to a name, then you
may as well just use def ...

def x(number):
    return number + 1

_________________________________________________________________
MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*. 
http://join.msn.com/?page=features/virus




More information about the Tutor mailing list