Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Processing each row independently using map function

Assuming the list:

[[2,4,0,8],[0,2,4,8],[8,0,2,4],[8,2,0,4]]

Hi guys, how do I use map function to process each element inside the row of the list?

For e.g. if I want to add the pairs of each list:

[[6,0,8,0],[2,0,12,0],[8,0,6,0],[10,0,4,0]]

Basically I don't know how to use the map function very well, never find very useful article about mapping. Appreciate if I can get some links. Thanks!!!

like image 229
shawnngtq Avatar asked May 08 '26 06:05

shawnngtq


2 Answers

It depends what is your function actually.

First: If the function say f accepts "list as complete row" then you can use:

m = [[6, 0, 8, 0], [2, 0, 12, 0], [8, 0, 6, 0], [10, 0, 4, 0]]
map(f, m)

Second: If function f accepts each element then you need to call nested map(), something like as below:

map(lambda r: map(f, r), m)

For this, you could also use list compression:

[map(f, r) for r in m] # I prefer this 

There are other tricks also, like you can use functools.partial Check this example.

from functools import partial
newf = partial(map, f)
map(newf, m)

Edit as response to comment:

Actually main thing is I want to know how to manipulate the elements in rows. Which mean I can also add all the element in for e.g. [2, 4, 0, 8] making it into [14, 0, 0, 0].

First, I have doubt why you wants to convert a list [2, 4, 0, 8] into [14, 0, 0, 0], why not to output just 14 — the sum, check if you can improve your algorithm/design of your program.

Anyways, in Python we have sum() function, read documentation sum(iterable[, start]), example (read comments):

>>> row =  [2,4,0,8] 
>>> sum(row)  # just outputs sum of all elements, in rows
14

So if you wants to pass "a list of lists" like m (you posted in question) then you can use map(sum, m) see below:

>>> m = [[6, 0, 8, 0], [2, 0, 12, 0], [8, 0, 6, 0], [10, 0, 4, 0]]
>>> map(sum, m)  # call sum for each row (a list) in `m`
[14, 14, 14, 14] # sum of all individual list's elements 

But if you wants to make a list like you asked then you can use + (list concatenate) operator with sum as below:

>>> row =  [2, 4, 0, 8] 
>>> [sum(row)] + [0,] * (len(row) -1 )
[14, 0, 0, 0]
>>> 

To understand this code piece:

  1. [sum(row)] means list of single element that is sum of row — [14].
  2. [0,] * (len(row) -1 ) makes a list of zeros of length one less then number of elements in row, check below code example:

     >>> row =  [2, 4, 0, 8]   # 4 elements 
     >>> [0,] * (len(row) -1 )
     [0, 0, 0] # 3 zeros
     >>> 
    

    The * operator similarly can be applied to tuple, string also.

  3. So from above, I make two separate lists and add + both finally as below:

    >>> row =  [2, 4, 0, 8] 
    >>> [sum(row)] + [0,] * (len(row) -1 )
    [14, 0, 0, 0]  # that is what you wants 
    

Now, you wants to execute [sum(row)] + [0,] * (len(row) -1 ) expression for each rowi ∈ m, then you can use lambda expression with map() as below:

>>> map(lambda row: [sum(row)] + [0,] * (len(row) -1 ), m)
[[14, 0, 0, 0], [14, 0, 0, 0], [14, 0, 0, 0], [14, 0, 0, 0]]

Better is to lambda expression a name:

>>> f = lambda r: [sum(r)] + [0,] * (len(r) -1 )
>>> f(row)
[14, 0, 0, 0]

then use map():

>>> map(f, m)
[[14, 0, 0, 0], [14, 0, 0, 0], [14, 0, 0, 0], [14, 0, 0, 0]]

Just as I suggested in first few lines in my answer. Again you can use list compression here in place of calling map() as:

>>> [f(r) for r in m]
like image 139
Grijesh Chauhan Avatar answered May 10 '26 18:05

Grijesh Chauhan


This is the not the cleanest solution, but it shows you how to use map on lists of lists.

However you want to define the addPairs method, it has to take a list as input, and return a list.

lst = [[2,4,0,8],[0,2,4,8],[8,0,2,4],[8,2,0,4]]

def addPairs(y):
   out = []
   for i in xrange(len(y)):
      if i % 2 == 0:
        out.append(y[i] + y[i+1])
      else: out.append(0)
   return out


output = map(addPairs, lst)
print output
like image 21
merlin2011 Avatar answered May 10 '26 19:05

merlin2011



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!