Multiply a tuple by a constant

Jeremy Sanders jeremy+plusnews at jeremysanders.net
Fri Feb 13 06:56:20 EST 2004


On Fri, 13 Feb 2004 02:20:06 -0800, Jay Davis wrote:

> I often need to multiply a tuple by a constant and the methods I use now
> are not very pretty.  The idea is to get a new tuple that is some factor
> times the start tuple, like  'newtup = .5 * oldtup'.
> 
> Is there a slick way to do this?

I suggest looking at Numarray - a very convenient way of doing maths on
sets of numbers. Of course if you don't do very much it's not worth doing
this.

http://www.stsci.edu/resources/software_hardware/numarray

You can do things like:

import numarray

a = numarray.array( (1.,2.,5.,-46.) )
a *= 0.5
b = a + 3.4

Jeremy




More information about the Python-list mailing list