File: map-nesting-example.py
#!/usr/bin/python
"""
========================================================================
Nested loops via map()/lambda, comprehension, and statements.
This code runs in Python 3.X and 2.X, and was a reply to a reader email.
Given:
x=[0, 1, 2], y=[100, 200, 300]
Output:
[100, 200, 300, 101, 201, 301, 102, 202, 302]
========================================================================
"""
from __future__ import print_function # for print() in 2.X
import sys
if sys.version[0] == '2': list = lambda x: x # list() moot in 2.X
print(sys.version.split()[0])
#-----------------------------------------------------------------------
# 1) original, from reader's email
#-----------------------------------------------------------------------
x=[0,1,2]
y=[100,200,300]
fn=lambda x:lambda y:x+y
n=[] #empty list to accumulate result
list(map(lambda x:n.extend(map(x,y)),map(fn,x)))
print(n)
#-----------------------------------------------------------------------
# 2) original, edited a bit for readability - whitespace, etc.
#-----------------------------------------------------------------------
x = [0, 1, 2]
y = [100, 200, 300]
fn = lambda x: lambda y: x + y
n = []
list(
map(
lambda x: n.extend(map(x, y)),
map(fn, x)
)
)
print(n)
#-----------------------------------------------------------------------
# 3) original, with operations decomposed for clarity
#-----------------------------------------------------------------------
X = [0, 1, 2]
Y = [100, 200, 300]
def fn(x):
def fm(y):
return x + y # a closure: remembers "x", expects "y"
return fm
temp = map(fn, X) # functions, each with a saved "x" value
n = []
for ft in temp: # for each of the closure functions:
sums = map(ft, Y) # apply function ft (with one "x") to each y
n.extend(sums)
print(n)
#-----------------------------------------------------------------------
# 4) a simpler (simplest?) alternative: nested loop comprehension
#-----------------------------------------------------------------------
X = [0, 1, 2]
Y = [100, 200, 300]
sums = [x + y for x in X for y in Y]
print(sums)
#-----------------------------------------------------------------------
# 5) equivalent to #4, but with full statements (may be slower?)
#-----------------------------------------------------------------------
X = [0, 1, 2]
Y = [100, 200, 300]
sums = []
for x in X:
for y in Y:
sums.append(x + y)
print(sums)
#-----------------------------------------------------------------------
# 6) nested map+lambda loops and append (nesting similar to statements)
#-----------------------------------------------------------------------
X = [0, 1, 2]
Y = [100, 200, 300]
n = []
t = list(map(lambda x: list(map(lambda y: n.append(x + y), Y)), X))
print(n)
#-----------------------------------------------------------------------
# 7) mapping sum() over items paired with a nested-loop comprehension
#-----------------------------------------------------------------------
X = [0, 1, 2]
Y = [100, 200, 300]
print( list(map(sum, [(x, y) for x in X for y in Y])) )