Python array multiply

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

I am getting an error while running this:

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

I tried to add float(), but this does not solve my problem. How can I fix this?

Jun 14, 2019 in Python by ana1504.k
• 7,910 points
3,382 views

1 answer to this question.

0 votes

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]
answered Jun 14, 2019 by SDeb
• 13,300 points

Related Questions In Python

+4 votes
7 answers
+2 votes
4 answers

python 2d array

You should make a list of lists, ...READ MORE

answered Oct 18, 2018 in Python by ritu
1,275 views
+1 vote
12 answers
0 votes
1 answer
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
0 votes
1 answer

Is arr.__len__() the preferred way to get the length of an array in Python?

my_list = [1,2,3,4,5,6,7] len(my_list) # 7 The same works for ...READ MORE

answered Oct 8, 2018 in Python by SDeb
• 13,300 points
947 views
0 votes
1 answer

Array values in Python

You can use the enumerate function and ...READ MORE

answered Nov 28, 2018 in Python by SDeb
• 13,300 points
814 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP