enhanced map function

Patrick zxpatric at gmail.com
Fri Mar 11 16:00:23 EST 2011


Hi,

The build-in map functions looks quite nice but it requests the
iterables to be of the same length or otherwises will file with None
(most of time fails the function). Just wondering if there are already
enhancement work done with it?

I did some simple code but it will handle list without nesting only.
I am looking for examples that could hand input of "a = [2,3], b=4"
and "a=[1,[2,3],4], b=[5,[6,7,8],9,10]". That means if the nesting
structure is the same, enhanced map function will automatically extend
the shorter list using the last element. Or if the input is a constant
at the first point, then it feeds constant value to all other iterable
combinations.

Any tip is appreciated.
-Patrick.



def Add (x,y):
    return x+y


def Bmap(function, *args):
	num = 0
	for iter in args[0:]:
		if num < len(iter):
			num = len(iter)

	nlist=[]
	for i in range(0,num,1):
		fargs = []
		for iter in args[0:]:
		    if len(iter) > i:
		        fargs.append(iter[i])
		        continue
		    fargs.append(iter[-1])

		nlist.append(function(*fargs))
	return nlist

if __name__ == '__main__':
    a = [2,3]
    b = [4,5,8]
    print Bmap (Add, a, b)



More information about the Python-list mailing list