Search code examples
pythonfloating-pointmultiplication

Python array multiply


hh=[[82.5], [168.5]]
N=1./5
ll=N*hh

What I'm doing wrong? I received error :

"can't multiply sequence by non-int of type 'float'"

I try to add float(), but this is not solve my problem;

I need to multiply each element in array... thanks to all


**Ok thanks for idea for number * array, but how to multiply array*array, I tried same as number*array, but have problems:

EDIT 2:**

hh=[[82.5], [168.5]]
N=zip(*hh)
ll = [[x*N for x in y] for y in hh]

???


Solution

  • When you multiply a sequence by X in Python, it doesn't multiply each member of the sequence - what it does is to repeat the sequence X times. That's why X has to be an integer (it can't be a float).

    What you want to do is to use a list comprehension:

    hh = [[82.5], [168.5]]
    N  = 1.0 / 5
    ll = [[x*N for x in y] for y in hh]