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
Python: TypeError: unsupported operand type(s) for /: 'float' and 'list'
提问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.
其中T和lamz是列表,flux是我通过插值制作的 ndarray。
Both Tand lamzare made by reading from a csv file and then appending the values to Tand lamzas floats.
二者T并lamz通过从csv文件读取,然后追加值到由T和lamz作为浮动。
回答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)

