Python:类型错误:不支持 / 的操作数类型:'float' 和 'list'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26948931/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:13:13  来源:igfitidea点击:

Python: TypeError: unsupported operand type(s) for /: 'float' and 'list'

pythonnumpyintegration

提问by Thomas Bennett

I'm trying to do a trapz integration, with a function using 2 lists and 1 numpy.ndarray. It won't work, and I can't work out whether I need to convert my lists to some other data type. I'm happy that the actual function is correct, but I'm not sure how to proceed?

我正在尝试使用 2 个列表和 1 个函数进行 trapz 集成numpy.ndarray。它不起作用,我无法确定是否需要将我的列表转换为其他数据类型。我很高兴实际功能是正确的,但我不确定如何继续?

The function is:

功能是:

BB_flux = trapz((1./lamz)*flux*T,x=lamz)/trapz((1./lamz)*T,x=lamz)

where Tand lamzare lists and fluxis a ndarray that I have made by interpolation.

其中Tlamz是列表,flux是我通过插值制作的 ndarray。

Both Tand lamzare made by reading from a csv file and then appending the values to Tand lamzas floats.

二者Tlamz通过从csv文件读取,然后追加值到由Tlamz作为浮动。

回答by Ben

Python doesn't support arithmetic operation (/,*,+,-) on lists. The operators + and * are list operations.

Python 不支持对列表进行算术运算 (/,*,+,-)。运算符 + 和 * 是列表操作。

Since you use numpy, you can convert your lists to numpy arrays.

由于您使用 numpy,因此您可以将列表转换为 numpy 数组。

If you want to keep lamz and T as lists you can use

如果您想将 lamz 和 T 保留为列表,您可以使用

w = numpy.asarray(T)/numpy.asarray(lamz)
BB_flux = trapz(w*flux,x=lamz)/trapz(w,x=lamz)