【问题标题】:scipy.optimize.newton gives TypeError: can only concatenate tuple (not "int") to tuplescipy.optimize.newton 给出 TypeError: can only concatenate tuple (not "int") to tuple
【发布时间】:2014-07-31 03:12:33
【问题描述】:

我的整个程序都是正确的(我已经在各个阶段进行了检查)。但是,此模块中突出显示的行返回错误:

TypeError: can only concatenate tuple (not "int") to tuple

我不知道为什么会这样。 funcPsat 返回浮点值。如果有任何有用的建议,我将不胜感激!

import scipy.optimize.newton as newton

def Psat(self, T):
    pop= self.getPborder(T)
    boolean=int(pop[0])
    P1=pop[1]
    P2=pop[2]
    if boolean:
        Pmin = min([P1, P2])
        Pmax = max([P1, P2])
        if Pmin > 0.0: 
            Pguess = 0.5*(Pmin+Pmax) 
        else:
            Pguess=0.5*Pmax
        solution = newton(self.funcPsat, Pguess, args=(T))   #error in this line
        return solution
    else:
        return None

【问题讨论】:

  • 您能提供完整的错误回溯吗? T 是什么?

标签: python function typeerror newtons-method


【解决方案1】:

我认为问题在于,根据文档

args: 元组,可选

函数调用中使用的额外参数。

args 参数应该是 tuple

只放括号是不行的;元组的语法是逗号。例如:

>>> T = 0
>>> type((T))
<type 'int'>
>>> type((T,))
<type 'tuple'>

试试:

solution = newton(self.funcPsat, Pguess, args=(T,))
                                              # ^ note comma

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    • 2022-11-01
    • 2021-12-18
    • 2020-11-21
    • 2021-08-18
    • 2021-02-02
    • 1970-01-01
    相关资源
    最近更新 更多