map() return of flat tuple list

Discussion in 'Python' started by Mirco Wahab, Jun 22, 2006.

  1. Mirco Wahab

    Mirco Wahab Guest

    Hi,

    I have a 2D array,
    maybe irregular, like

    arr = [[2,2,2,2],
    [2,2,2,2],
    [2,2,2,2]]

    if tried to pull an index list
    (tuples or array elements) of
    all positions - via the map funtion,
    but failed.

    I tried to get sth. like
    [
    [0,0],
    [0,1],
    [0,2],
    ...
    ]
    for each element which really exists
    in the 2D array above.

    What I really got, was another in-
    direction for each row of arr
    [
    [(0, 0), (0, 1), (0, 2), (0, 3)],
    [(1, 0), (1, 1), (1, 2), (1, 3)],
    ...
    ]

    How can I flatten the list in place by map(),
    so that the outer map hull would collect
    [i,j] flat?

    This is what I tried:

    id = map(lambda i: map(lambda j: (i,j), range(len(arr))), range(len(arr)))
    print id



    I tried the map solution because I'd make a
    shot at another posting (find min of 2D array),
    what I intended to solve by decorate/undecorate
    sort.

    But I didn't manage to create the flat
    coordinate list in one map(map)-stroke ;-)

    Regards

    Mirco
     
    Mirco Wahab, Jun 22, 2006
    #1
    1. Advertising

  2. Mirco Wahab

    Guest

    Maybe you want something like this (but this doesn't use map):

    def indexes(m):
    return [(r,c) for r, row in enumerate(m) for c in xrange(len(row))]

    m1 = [[2,2,5],
    [2,2],
    [2,2,2,2]]

    m2 = [[],
    [2],
    [1,2,3,4]]

    print indexes(m1)
    print indexes(m2)

    Output:
    [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (2, 0), (2, 1), (2, 2), (2,
    3)]
    [(1, 0), (2, 0), (2, 1), (2, 2), (2, 3)]

    Bye,
    bearophile
     
    , Jun 22, 2006
    #2
    1. Advertising

  3. Mirco Wahab

    Mirco Wahab Guest

    Thus spoke (on 2006-06-23 00:57):

    > Maybe you want something like this (but this doesn't use map):
    > [(r,c) for r, row in enumerate(m) for c in xrange(len(row))]


    Ahh, its a 'list comprehension', nice. Now,
    lets see how the decorate/undecorate sort
    turns out to look in Python:

    arr = [
    [3,3,3,3],
    [3,3,3,1],
    [3,3,3,3] ]


    print \
    sorted(
    [ (j,i) for j, row in enumerate(arr) for i in xrange(len(row)) ],
    lambda a,b: (arr[a[0]][a[1]] - arr[b[0]][b[1]])
    )[ 0 ]


    ==> prints indices: (1,3)

    He, this looks more like Haskell than like
    Python (for me, it looks awful ;-)

    I'll try to come up with at least one
    map inside the comprehension, if that
    works - just to avoid the dual for ;-)

    Reagrds and thanks

    Mirco
     
    Mirco Wahab, Jun 23, 2006
    #3
  4. Mirco Wahab

    Guest

    Mirco:
    >He, this looks more like Haskell than like Python (for me, it looks awful ;-)


    Maybe this is more readable:

    ar = [[3,3,3,3],
    [3,3,3,1],
    [3,3,4,3]]

    print sorted( [(r,c) for r,row in enumerate(ar) for c in
    xrange(len(row))],
    key=lambda (r,c): ar[r][c]
    )[0]

    I don't know if operator.itemgetter can be used here, I think it's too
    much complex.

    With python 2.5 you can probably simplify it a little (and speed it up)
    with something like:

    print min( [ (r,c) for r,row in enumerate(ar) for c in xrange(len(row))
    ],
    key=lambda (r,c): arr[r][c]
    )

    Bye,
    bearophile
     
    , Jun 23, 2006
    #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. Michal Mikolajczyk
    Replies:
    1
    Views:
    812
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    962
    Jeff Epler
    Apr 20, 2004
  3. Will McGugan

    flat tuple

    Will McGugan, Sep 21, 2004, in forum: Python
    Replies:
    6
    Views:
    360
    Peter Hansen
    Sep 21, 2004
  4. Davy
    Replies:
    3
    Views:
    1,893
    Wildemar Wildenburger
    Nov 7, 2007
  5. Jeff Nyman
    Replies:
    8
    Views:
    388
    Terry Reedy
    Jun 5, 2008
Loading...

Share This Page