Operation on 2d array columns

Without numpy it can be done like this:

map(lambda x: x[:2] + [1] + x[3:], array)

map(lambda x: x[:2] + my_func(x[2]) + x[3:], array)

It would be very simple in numpy and you can do it with a simple assignment :

>>> numpy.array[:,column_number]=value

But if you are looking for a python approach you can use zip function and itertools.repeat():

>>> from itertools import repeat
>>> def replacer(l,index,value):
...     z=zip(*l)
...     z[index]=list(repeat(value,len(l)))
...     return zip(*z)

Demo:

>>> l=[range(4) for _ in range(3)]
>>> replacer(l,2,'*')
[(0, 1, '*', 3), (0, 1, '*', 3), (0, 1, '*', 3)]

Note that since in python 3.X zip returns an iterator you can use list function to return a list also since iterators doesn't support indexing inside the function you need to call the list too.

>>> def replacer(l,index,value):
...     z=list(zip(*l))
...     z[index]=list(repeat(value,len(l)))
...     return zip(*z)

>>> list(replacer(l,2,'*'))
[(0, 1, '*', 3), (0, 1, '*', 3), (0, 1, '*', 3)]

You can do this easily with numpy arrays. Example -

In [2]: import numpy as np

In [3]: na = np.array([[1,2,3],[3,4,5]])

In [4]: na
Out[4]:
array([[1, 2, 3],
       [3, 4, 5]])

In [5]: na[:,2] = 10

In [6]: na
Out[6]:
array([[ 1,  2, 10],
       [ 3,  4, 10]])

In [7]: na[:,2]
Out[7]: array([10, 10])

In [8]: def func(a):
   ...:     for i,x in enumerate(a):
   ...:         a[i] = x + 1
   ...:

In [9]: na
Out[9]:
array([[ 1,  2, 10],
       [ 3,  4, 10]])

In [10]: func(na[:,1])

In [11]: na
Out[11]:
array([[ 1,  3, 10],
       [ 3,  5, 10]])

You can find more details about this here . Please do be careful , for numpy arrays, as stated in documentation -

All arrays generated by basic slicing are always views of the original array.

This is why when changing the sliced array inside the function, the actual array got changed.

Tags:

Python

Arrays

2D