[Numpy-discussion] setting element

Gabriel Gellner ggellner at uoguelph.ca
Wed Nov 12 13:33:52 EST 2008


On Wed, Nov 12, 2008 at 09:43:34AM -0800, Charles سمير Doutriaux wrote:
> Hello,
> 
> I'm wondering if there's aquick way to do the following:
> 
> s[:,5]=value
> 
> in a "general" function
> def setval(array,index,value,axis=0):
> 	## code here
> 
> The issue is to put enough ":" before the index value inside the  
> square bracket of the assignement.
> 
Make some slice objects!

def setval(array, index, value, axis=0):
    key = [slice(None)]*len(array.shape)
    key[axis] = index
    array[key] = value

Gabriel




More information about the NumPy-Discussion mailing list