best way to do some simple tasks
elechak at bigfoot.com
Thu Jan 30 00:31:59 CET 2003
I am transitioning from perl to python. I find myself trying to do
many things in a perlish way. Perl made many common tasks such as
string manipulations easy (in a perlish sort of way). So I was
wondering if someone could suggest the "pythonish" way of doing these
things. I have read tons of documentation and can do all of the
following tasks, but my code does not look "professional".
1) String interpolation of lists and dictionaries
What is the correct and best way to do this?
print "%(x)i blah" % vars() # <- does not work
2) Perform a method on each element of an array
a= [" hello ", " there "]
how do I use the string's strip() method on each element of the
a = map((lambda s: s.strip()), a) # looks ugly to me
for e in a: # this looks even worse
3) What is the best way to do this?
how do I get c=a+b (c=[2,3,4])?
If I wanted to use reduce(), how do I shuffle the lists?
4) Why does this work this way?
for h in d:
>>>[1, 2, 3, 4, 5]
Is there a python reason why h is a copy not a reference to the
element in the list? In perl the h would be a ref. If d was a tuple
I could understand it working this way.
More information about the Python-list