简体   繁体   English

Python不能将序列乘以'float'类型的非int

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

I am trying to evaluate a formula, np is numpy : 我正在尝试评估一个公式, npnumpy

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? 我怎么能克服这个?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM