[Numpy-discussion] setting element

Ryan May rmay31 at gmail.com
Wed Nov 12 13:34:51 EST 2008


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

Assuming that axis specifies where the index goes, that would be:

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

(Adapted from the code for numpy.diff)

Ryan

-- 
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma



More information about the NumPy-Discussion mailing list