【问题标题】:TypeError: unsupported operand type(s) for /: 'list' and 'int'TypeError: 不支持的操作数类型 /: 'list' 和 'int'
【发布时间】:2016-05-31 20:36:24
【问题描述】:

我收到以下错误:

/ 不支持的操作数类型:'list' 和 'int'

我该如何解决这个问题?任何想法?

这是我的代码:

def func(xdata_1,cc,dd,gg):
    return cc*(xdata_1**(dd))*
           (10**(-1.572*gg*( (185/((xdata_1/420)**2 + (420/xdata_1)**2 + 90 )) )

params,pcov = curve_fit(func,xdata_1,ydata_1,
                        sigma=err_1, absolute_sigma=True)

fc_1 = func(xdata_1, *params)

【问题讨论】:

  • 听起来传递给func 的参数之一是一个列表。所以,找出哪一个(你可以在调用func之前打印它们),然后修复它。
  • so many other questions的可能重复
  • 您的代码甚至在语法上都不正确,它不可能引发 TypeError。

标签: python function error-handling typeerror


【解决方案1】:

检查所有变量的数据类型,即xdata_1,cc,dd,gg

1.如何检查变量的类型:

使用 'type` 内置函数获取变量的类型。

演示:

>>> d
[1, 2, 3]
>>> type(d)
<type 'list'>
>>> 

2。关于异常:

当我们对list 和int 变量进行/ 操作时会出现此异常。

演示:

>>> d = [1,2,3]
>>> d/4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'list' and 'int'
>>> 

3。提供意见:

最好在问题中提供输入详细信息,即xdata_1 和params 的值,以便我们可以告诉您代码错误的地方。

【讨论】:

    猜你喜欢
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    • 2014-12-28
    • 2021-09-21
    • 1970-01-01
    相关资源
    最近更新 更多