map() return of flat tuple list

M

Mirco Wahab

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
 
B

bearophileHUGS

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
 
M

Mirco Wahab

Thus spoke (e-mail address removed) (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
 
B

bearophileHUGS

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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top