Python array is multiplied

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

What am I doing wrong? I got an error:

"cannot multiply the sequence by non-int type 'float'"

I am trying to add float (), but this does not solve my problem;

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


** Well, thanks for the idea for the array number *, but how to multiply the array * array, I tried the same as the array number *, but had problems:

EDIT 2: **

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

???

+5
source share
3 answers

X Python, - , , X . X ( float).

, :

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

Python :

>>> [2] * 3
[2, 2, 2]

int.

, , - - map .

>>> list(map(lambda x: x * 2, [2, 2]))
[4, 4]
>>> [x * 2 for x in [2, 2]]
[4, 4]

, .

(x * 2 for x in [2, 2])

Haskellish ( ):

>>> import operator
>>> from functools import partial, reduce
>>> add = partial(operator.mul, 2)
>>> list(map(add, [2,2]))
[4, 4]
+4

numpy .

hh = numpy.asarray([[82.5], [168.5]])
>>> N=1.0/5
>>> ll=N*hh
>>> ll
array([[ 16.5],
       [ 33.7]])
+4

All Articles