[Tutor] summing lists
Dave Angel
d at davea.name
Tue Apr 10 17:45:13 CEST 2012
On 04/10/2012 11:28 AM, Hs Hs wrote:
> sorry I did this following, please advise if any better way exist:
>
You top-posted, so we lose the context you were using.
You never really say why you're using lists with exactly one element in
them, but presuming that the real lists consist of something like
a = [4, "Tom Jones"]
b = [12, "Peter Rabbit", "March Hare"]
I'll follow your convention of assuming only the zeroth element is
relevant to your 'definition of add'.
> gc = lambda x,y: x[0]+y[0]
>
> atgc = lambda x,y,k,l: x[0]+y[0]+k[0]+l[0]
>
>
No point in using lambda for that purpose. Since you are not trying to
make it anonymous, you could simply use
def gc(x, y):
return x[0] + y[0]
for example.
But the more interesting question is how to combine the two functions,
and make one which can take a variable number of items.
def gc(*many):
return sum( [ x[0] for x in many ] )
--
DaveA
More information about the Tutor
mailing list