Python array multiply

后端 未结 3 1853
温柔的废话
温柔的废话 2020-12-20 12:14
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 typ

3条回答
  •  渐次进展
    2020-12-20 13:07

    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]
    

提交回复
热议问题