Python 不能将序列乘以“float”类型的非整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16994648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python can't multiply sequence by non-int of type 'float'
提问by y33t
I am trying to evaluate a formula, npis numpy:
我正在尝试评估一个公式,np是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?
我怎样才能克服这个问题?
采纳答案by Mike Müller
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.
在与 相乘之前计算除法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

