Python array multiply

0 votes
hh=[[82.5], [168.5]]

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 in Python by ana1504.k
• 7,870 points

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 by SDeb
• 13,180 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
+2 votes
11 answers
0 votes
1 answer
+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 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 337 views
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,180 points
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,180 points