Mailing List Archive

[Question] Functional style with lists
Hi,

i like to program in functional style, for instance, to get the sum of
square values for a range i do :
>>> reduce(lambda x,y:x+y,map(lambda x:x*x,range(10)))
285
>>>

Ok, but i've a problem, how to access a list element in this fashion,
for instance:
>>> L = [ ("John",40), ("Monthy",35)]

I would like to sum the numeric values by using map.
I've tried this:
>>> map(lamda x,y:x+y,map(L.getitem,(1,)*len(L)))

But of course it doesn't work...

Does anybody has an idea to help me ?

--
- - --
David Berthelot Contact at:
3rd year PhD Student | LIRMM
Email: berthelo@lirmm.fr | 161, Rue Ada
Tel : 33 4 67 418 577 | 34392 MONTPELLIER Cedex 5
Fax : 33 4 67 418 500 | FRANCE
[Question] Functional style with lists [ In reply to ]
David Berthelot writes:
>I would like to sum the numeric values by using map.
>I've tried this:
>>>> map(lamda x,y:x+y,map(L.getitem,(1,)*len(L)))
>But of course it doesn't work...

The reduce() built-in does this sort of thing. Try:
>>> reduce(lambda total,(name,amt): total + amt, L, 0)
75

--
A.M. Kuchling http://starship.python.net/crew/amk/
I'm an excuse for medical experiments and art theory. You must get me out of
here and out of the hospital.
-- Peter Greenaway, _A Zed and Two Noughts_ (1986)
[Question] Functional style with lists [ In reply to ]
David Berthelot <berthelo@lirmm.fr> writes:

| Hi,
|
| i like to program in functional style, for instance, to get the sum of
| square values for a range i do :
| >>> reduce(lambda x,y:x+y,map(lambda x:x*x,range(10)))
| 285
| >>>
|
| Ok, but i've a problem, how to access a list element in this fashion,
| for instance:
| >>> L = [ ("John",40), ("Monthy",35)]
|
| I would like to sum the numeric values by using map.
| I've tried this:
| >>> map(lamda x,y:x+y,map(L.getitem,(1,)*len(L)))
|
| But of course it doesn't work...

I think you meant 'reduce' instead of 'map' at the beginning, as in
your first example.

Also, of course, 'lamda' should be 'lambda'.

To solve your problem, instead of trying to use __getitem__ somehow,
you can just write another lambda:

>>> L = [ ("John",40), ("Monthy",35)]
>>> reduce (lambda x,y:x+y, map(lambda l:l[1], L))
75

In this case the story has a happy ending. But be aware that Python's
support for functional programming is not very complete, and often it
is harder to do things functionally than to do them with iteration.

--
Dan Schmidt -> dfan@harmonixmusic.com, dfan@alum.mit.edu
Honest Bob & the http://www2.thecia.net/users/dfan/
Factory-to-Dealer Incentives -> http://www2.thecia.net/users/dfan/hbob/
Gamelan Galak Tika -> http://web.mit.edu/galak-tika/www/
[Question] Functional style with lists [ In reply to ]
You might want to look into the operator module. Instead of

>>> map(lamda x,y:x+y,map(L.getitem,(1,)*len(L)))

the commands

>>> L = [ ("John",40), ("Monthy",35)]
>>> import operator
>>> reduce (operator.add, \
map (operator.getitem, L, len(L) * (1, )), \
0)
75

perhaps does what you expected. Gets messy very fast, though...

Alex.
[Question] Functional style with lists [ In reply to ]
You might want to look into the operator module. Instead of

>>> map(lamda x,y:x+y,map(L.getitem,(1,)*len(L)))

the commands

>>> L = [ ("John",40), ("Monthy",35)]
>>> import operator
>>> reduce (operator.add, \
map (operator.getitem, L, len(L) * (1, )), \
0)
75

perhaps do what you expected. Gets messy very fast, though...

Alex.