Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

numpy

I am trying to evaluate a formula, np is numpy:

Ds = pow(10,5)
D = np.linspace(0, pow(10,6), 100)
alpha=1.44
beta=0.44
A=alpha*(D/Ds)
L=1.65
buf2=L/4.343
buf=pow(-(alpha*[D/Ds]),beta)
value=exp(buf)

and then I will plot this data but I get:

buf=pow(-(alpha*[D/Ds]),beta)
TypeError: can't multiply sequence by non-int of type 'float'

How can I overcome this?

like image 309
y33t Avatar asked Jun 08 '13 00:06

y33t


1 Answers

Change:

buf=pow(-(alpha*[D/Ds]),beta)

to:

buf=pow(-(alpha*(D/Ds)),beta)

This:

[D/Ds]

gives you list with one element.

But this:

alpha * (D/Ds)

computes the divisions before the multiplication with alpha.

You can multiply a list by an integer:

>>> [1] * 4
[1, 1, 1, 1]

but not by a float:

[1] * 4.0
TypeError: can't multiply sequence by non-int of type 'float'

since you cannot have partial elements in a list.

Parenthesis can be used for grouping in the mathematical calculations:

>>> (1 + 2) * 4
12
like image 60
Mike Müller Avatar answered Nov 15 '22 18:11

Mike Müller