Do I need a nested lambda to do this?

R

raoul

I can't figure this one out. Trying to be unnecessarily functional I
suspect.

I have the following lists.

vals = [1.000,2.344,4.2342]
tab = [((0,1),(0,3),(0,4)),
((2,2),(3,0),(3,9)),
((3,4),(6,3),(7,1))]

I'm trying to create a one liner using map/reduce/lambda/zip(* etc to
do replace the first element of each pair in each row by the the values
in vals as follows:

tab = [((1.000,1),(1.000,3),(1.000,4)),
((2.344,2),(2.344,0),(2.344,9)),
((4.2342,4),(4.2342,3),(4.2342,1))]

I'm pretty sure there's a oneline way to do this. Naturally, one could
loop mindlessly but this is my self-imposed programming challenge for
the day and I'm not feeling much love from my python.
 
R

R. C. James Harlow

I can't figure this one out. Trying to be unnecessarily functional I
suspect.

With list comprehensions:

Python 2.3.4 (#1, Mar 26 2005, 20:54:10)
[GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
vals = [1.000,2.344,4.2342]
tabs = [((0,1),(0,3),(0,4)),
.... ((2,2),(3,0),(3,9)),
.... ((3,4),(6,3),(7,1))]
[(tuple([(vals[index],subtab[1]) for subtab in tab])) for index,tab in
enumerate(tabs)]
[((1.0, 1), (1.0, 3), (1.0, 4)), ((2.3439999999999999, 2),
(2.3439999999999999, 0), (2.3439999999999999, 9)), ((4.2342000000000004, 4),
(4.2342000000000004, 3), (4.2342000000000004, 1))]
 
B

Bill Mill

I can't figure this one out. Trying to be unnecessarily functional I
suspect.

With list comprehensions:

Python 2.3.4 (#1, Mar 26 2005, 20:54:10)
[GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
vals = [1.000,2.344,4.2342]
tabs = [((0,1),(0,3),(0,4)),
... ((2,2),(3,0),(3,9)),
... ((3,4),(6,3),(7,1))]
[(tuple([(vals[index],subtab[1]) for subtab in tab])) for index,tab in
enumerate(tabs)]
[((1.0, 1), (1.0, 3), (1.0, 4)), ((2.3439999999999999, 2),
(2.3439999999999999, 0), (2.3439999999999999, 9)), ((4.2342000000000004, 4),
(4.2342000000000004, 3), (4.2342000000000004, 1))]

Slightly nicer, I think:
vals [1.0, 2.3439999999999999, 4.2342000000000004]
tabs [((0, 1), (0, 3), (0, 4)), ((2, 2), (3, 0), (3, 9)), ((3, 4), (6, 3), (7, 1))]
[tuple([(v, t[1]) for t in tab]) for v, tab in zip(vals, tabs)]
[((1.0, 1), (1.0, 3), (1.0, 4)),
((2.3439999999999999, 2), (2.3439999999999999, 0),
(2.3439999999999999, 9)),
((4.2342000000000004, 4), (4.2342000000000004, 3),
(4.2342000000000004, 1))]

Peace
Bill Mill
bill.mill at gmail.com
 
K

Kent Johnson

raoul said:
I can't figure this one out. Trying to be unnecessarily functional I
suspect.

I have the following lists.

vals = [1.000,2.344,4.2342]
tab = [((0,1),(0,3),(0,4)),
((2,2),(3,0),(3,9)),
((3,4),(6,3),(7,1))]

I'm trying to create a one liner using map/reduce/lambda/zip(* etc to
do replace the first element of each pair in each row by the the values
in vals as follows:

tab = [((1.000,1),(1.000,3),(1.000,4)),
((2.344,2),(2.344,0),(2.344,9)),
((4.2342,4),(4.2342,3),(4.2342,1))]

I don't know about map/etc but it's pretty easy using list comp and gen exp:
print [ tuple( (val, t[1]) for t in tt ) for val, tt in zip(vals, tab) ]

Kent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,236
Messages
2,571,184
Members
47,820
Latest member
HortenseKo

Latest Threads

Top