enhanced map function

Discussion in 'Python' started by Patrick, Mar 11, 2011.

  1. Patrick

    Patrick Guest

    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)
    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)
    Patrick, Mar 11, 2011
    #1
    1. Advertising

  2. On Fri, 11 Mar 2011 13:00:23 -0800, Patrick wrote:

    > 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?



    That has been fixed in Python 3.1:

    >>> from operator import add
    >>> list(map(add, [1,2,3], [1,2,3,4,5]))

    [2, 4, 6]

    Starting from Python 2.3, the itertools module has had a function imap
    with the same behaviour:

    >>> from operator import add
    >>> list(itertools.imap(add, [1,2,3], [1,2,3,4,5]))

    [2, 4, 6]



    > I did some simple code but it will handle list without nesting only.


    Surely that is a good thing. If you have a mix of nested and non-nested
    data, that tells me your data is badly organized and needs to be fixed.


    > I am looking for examples that could hand input of "a = [2,3], b=4"


    Using a small helper function:

    import itertools

    def make_iterable(obj):
    try:
    iter(obj)
    except TypeError:
    obj = itertools.cycle([obj])
    return obj

    def enhanced_map(func, *args):
    args = map(make_iterable, args)
    return list(itertools.imap(func, *args))


    WARNING: this is dangerous. If none of the arguments are iterable, e.g.
    you call enhanced_map(operator.add, 2, 3), this will happily loop forever
    building a larger and larger list, until you run out of memory or kill
    the Python process.


    > 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.


    It isn't clear what you want here. Are you expecting this enhanced map to
    recursively drop down into each layer of sub-sequences? That is:

    enhanced_map([1, [2,3, [4,5], 6], 7], [8, [7,6, [5,4], 3], 2])

    should be the same as

    map([1, 2, 3, 4, 5, 6, 7], [8, 7, 6, 5, 4, 3, 2])

    or something different?


    What do you expect to happen if the sub-sequences don't match up exactly?
    E.g. a = [1, 2, [3, 4]]; b = [1, [2, 3], 4]

    What do you expect to happen if the shorter list is empty?
    E.g. a = [1, 2, [3, 4], 5]; b = [1, 2, [], 3]

    This will get really messy fast. My advice is to forget about this as a
    bad idea, and instead concentrate on making sure your data isn't so badly
    arranged in the first place.



    --
    Steven
    Steven D'Aprano, Mar 12, 2011
    #2
    1. Advertising

  3. Patrick

    Patrick Guest

    Steven,

    Thanks for the info of itertools. It is a great start for me. Overall,
    I agree with you that it is really the user data needs to be sorted
    out. However, novice users may need help on certain patterns such as
    "a=[1,[2,3],4], b=[5,[6,7,8],9,10]". We could just draw our line
    saying that similarly nested inputs could be adjusted even though the
    members aren't exactly on one-to-one mapping and we won't getting any
    deeper for complicated cases such as "a = [1, 2, [3, 4]]; b = [1, [2,
    [3,4]], [4,5], 6]".

    > enhanced_map([1, [2,3, [4,5], 6], 7], [8, [7,6, [5,4], 3], 2])
    > should be the same as
    > map([1, 2, 3, 4, 5, 6, 7], [8, 7, 6, 5, 4, 3, 2])


    I don't expect the drop. The original nested structure is very
    important.


    > What do you expect to happen if the sub-sequences don't match up exactly?
    > E.g. a = [1, 2, [3, 4]]; b = [1, [2, 3], 4]
    >
    > What do you expect to happen if the shorter list is empty?
    > E.g. a = [1, 2, [3, 4], 5]; b = [1, 2, [], 3]


    There are modes called "shortest" and "longest" (and
    "AllCombination"/"Cross" which is more complex).

    For case a = [1, 2, [3, 4],4]; b = [1, [2, 3], 4,5]

    shortest:
    a will be adjusted to [1, [3, 4],4]
    b will be adjusted to [1, [2, 3],4]

    longest:
    a will be adjusted to [1, 2,[3, 4],4,4]
    b will be adjusted to [1, 1,[2, 3],4,5]

    As I said previously, the enhance_map function will only handle
    limited "unmatch" cases and the line will need to be drawn carefully.

    Thanks
    -Patrick.
    Patrick, Mar 17, 2011
    #3
  4. On Thu, 17 Mar 2011 08:31:28 -0700, Patrick wrote:

    > Steven,
    >
    > Thanks for the info of itertools. It is a great start for me. Overall, I
    > agree with you that it is really the user data needs to be sorted out.
    > However, novice users may need help on certain patterns such as
    > "a=[1,[2,3],4], b=[5,[6,7,8],9,10]".



    You have misunderstood me. I'm not saying that you should force the users
    to clean up the data (although of course you could do that), but that you
    should do so before handing it to map.

    Rather than putting all the smarts into enhanced_map, and having it
    understand what to do with mismatched nested lists, deep nesting,
    integers where you would expect a list, etc., you should write another
    function that takes the user's data and adjusts it to some known,
    consistent format, and then pass that on to map. Don't have one function
    try to do too much.



    --
    Steven
    Steven D'Aprano, Mar 18, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Remi Bastide
    Replies:
    5
    Views:
    13,530
    elizas
    Mar 29, 2010
  2. Christian Schlichtherle
    Replies:
    8
    Views:
    465
    John Currier
    Jul 5, 2005
  3. Marc Schellens

    enhanced file info

    Marc Schellens, Aug 24, 2004, in forum: C++
    Replies:
    2
    Views:
    321
    Thomas Matthews
    Aug 24, 2004
  4. Andrew Koenig
    Replies:
    46
    Views:
    972
    Peter Maas
    Feb 24, 2004
  5. Steven D'Aprano

    Enhanced dir() function

    Steven D'Aprano, Jul 1, 2011, in forum: Python
    Replies:
    7
    Views:
    290
    Chris Angelico
    Jul 13, 2011
Loading...

Share This Page