best way to do some simple tasks
phr-n2003b at NOSPAMnightsong.com
Thu Jan 30 01:01:44 CET 2003
elechak at bigfoot.com (Erik Lechak) writes:
> 1) String interpolation of lists and dictionaries
> What is the correct and best way to do this?
> x =[1,2,3,4]
> print "%(x)i blah" % vars() # <- does not work
print "%d blah" % x
> 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
a = [strip(x) for x in a]
> 3) What is the best way to do this?
> how do I get c=a+b (c=[2,3,4])?
c = [a[i] + b[i] for i in range(len(a))]
> 4) Why does this work this way?
> for h in d:
> print 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.
Python doesn't have references.
More information about the Python-list