1

I am new to python map and reduce framework. Say I have the following matrix:

m = [[1,2,3],[5,6,7],[9,10,11]]

and I want to use a map function with a lambda function to create a pair of each row with a row index at the front. Desired output like the following:

[(0, [1, 2, 3]),
 (1, [5, 6, 7]),
(2, [9, 10, 11])]

I tried the following but I do not know how to iterate the row index. I know that I should replace the 0 in x[0] with something else, but I do not know what.

map(lambda x: (x.index(x[0]), x) ,m) 

# [(0, [1, 2, 3]), (0, [5, 6, 7]), (0, [9, 10, 11])]
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
lll
  • 1,049
  • 2
  • 13
  • 39

3 Answers3

0

a simple list comprehension with enumerate

l = [[1,2,3],[5,6,7],[9,10,11]]
print([(r, row) for r,row in enumerate(l)])
print(list(map(lambda x: (x), enumerate(l))))

output

[(0, [1, 2, 3]), (1, [5, 6, 7]), (2, [9, 10, 11])]
[(0, [1, 2, 3]), (1, [5, 6, 7]), (2, [9, 10, 11])]
Rob Raymond
  • 29,118
  • 3
  • 14
  • 30
-1

You were trying to get the index of first value of each element(x[0]) in the same element(x): x.index(x[0]). So you can change that to m.index(x):

m = [[1,2,3],[5,6,7],[9,10,11]]

print(list(map(lambda x: (m.index(x), x) ,m) ))

Output:

[(0, [1, 2, 3]), (1, [5, 6, 7]), (2, [9, 10, 11])]
MrNobody33
  • 6,413
  • 7
  • 19
-1

You do get the desired output using enumerate on the 2-d list m. You then need to assign the map object back to m by converting it into a list. You can do it as follows -

APPROACH - 1

m = [[1,2,3],[5,6,7],[9,10,11]]

m=list(map(lambda x: (x[0],x[1]), enumerate(m)))
print(m)

OUTPUT :

[(0, [1, 2, 3]), (1, [5, 6, 7]), (2, [9, 10, 11])]

APPROACH - 2

Another possible way of doing it would be as follows -

m = [[1,2,3],[5,6,7],[9,10,11]]

m=list(map(lambda x: (m.index(x),x), m)) 
# finding index in m using m.index(x) instead of x.index(x[0]) which would always return 0
print(m)

OUTPUT :

[(0, [1, 2, 3]), (1, [5, 6, 7]), (2, [9, 10, 11])]

What you were trying to do with (x.index(x[0]) finds the element in the elements of your original 2-d list which would always give you 0 (since you are finding the 0th index element in the list which would obviously give 0).

What you should instead be doing is finding the index of element over which you are mapping in the map (and not in x-which is elements of m).


APPROACH - 3 (better alternative than first two versions)

A better solution than the above approaches(as suggested in this answer) is using the list enumeration. So, the solution using list enumeration is as follows -

m = [[1,2,3],[5,6,7],[9,10,11]]

m=[(index,element) for index,element in enumerate(m)]
print(m)

OUTPUT :

[(0, [1, 2, 3]), (1, [5, 6, 7]), (2, [9, 10, 11])]

This approach turns out to be somewhat faster than using the map with lambda approach. You can read more about list enumeration here

APPROACH - 4 (one-liner)

Since enumerate() returns an enumerate object which contains index and the element itself, the approach-3 could be modified to a shorter and simpler version as follows -

m = [[1,2,3],[5,6,7],[9,10,11]]
print(list(enumerate(m)))

OUTPUT :

[(0, [1, 2, 3]), (1, [5, 6, 7]), (2, [9, 10, 11])]
Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
  • Hey @Abhishek, your second solution it's actually my answer. :/ Please, delete it or edit it to give a different solution. – MrNobody33 Jul 15 '20 at 19:18
  • I posted first, so please edit you answer. And If you downvoted my answer(just guessing), maybe could you an explanation of why. @Abhishek – MrNobody33 Jul 15 '20 at 19:25
  • Thanks for edited it. Now, your second solution it's actually the solution of @Rob Raymond :/ – MrNobody33 Jul 15 '20 at 19:27
  • Sure, we can upvote each other once we got different solutions, because there are not similar approaches, there are the same solutions , maybe with a different explanation, but it's still the same solution. – MrNobody33 Jul 15 '20 at 19:36
  • You have two options different to the first answers, why don't you just leave those two and problem solved? @Abhishek – MrNobody33 Jul 15 '20 at 19:42
  • @MrNobody33 Why should I delete a correct answer just because it matches your version? Maybe someone in the future finds my answer explanation to be helpful. – Abhishek Bhagate Jul 15 '20 at 19:45
  • *It matches to your version*? Really? Two of your solutions were already answered, before you published yours. Actually, your "versions" matched with our original solutions. – MrNobody33 Jul 15 '20 at 20:17