Python 3.x 更改函数参数以生成多个输出时出错
当我在函数更改一个参数以获得第二个结果集后取消注释此行时:Python 3.x 更改函数参数以生成多个输出时出错,python-3.x,function,Python 3.x,Function,当我在函数更改一个参数以获得第二个结果集后取消注释此行时: weights2 = weights(flags, 0, .4, .3, .3, 0, .4) ## This line is the problem 我收到这个错误: TypeError:“tuple”对象不可调用 输入: 功能: 这条线用从函数返回的元组权重来阴影函数权重 当您在第二次调用元组时尝试调用函数时,因此错误元组是不可调用的 例如,必须为函数或返回值使用不同的名称 calculated_weights1 = weight
weights2 = weights(flags, 0, .4, .3, .3, 0, .4) ## This line is the problem
我收到这个错误:
TypeError:“tuple”对象不可调用
输入:
功能:
这条线用从函数返回的元组权重来阴影函数权重
当您在第二次调用元组时尝试调用函数时,因此错误元组是不可调用的
例如,必须为函数或返回值使用不同的名称
calculated_weights1 = weights(flags, 0, .4, .025, .3, 0, .4)
calculated_weights2 = weights(flags, 0, .4, .3, .3, 0, .4)
将结果名称更改为out1、out2等后没有错误
import pandas as pd
import numpy as np
flags = pd.DataFrame({'Date': ['2017-11-01','2017-12-01','2018-01-01'],
'flag_11': [2, 2, 2],
'flag_12': [2, 2, 2]})
flags = flags.set_index('Date')
print(flags)
def weights(dfin, wt1, wt2, wt3, wmin06, wmin1012, wmax1012):
dfin = pd.DataFrame(dfin)
dfout = pd.DataFrame()
dfcum = pd.DataFrame()
mapping = {1:wt1,2:wt2,3:wt3}
dfout['flag_12']=dfin['flag_12'].replace(mapping)
dfcum['flag_12']=dfin['flag_12'].replace(mapping)
i = 11
conditions = [
(dfin["flag_{}".format(i)]==1) & (wt1 > (wmax1012 - dfout["flag_{}".format(i+1)])),
(dfin["flag_{}".format(i)]==1) & (wt1 < (wmin1012 - dfout["flag_{}".format(i+1)])),
(dfin["flag_{}".format(i)]==1),
(dfin["flag_{}".format(i)]==2) & (wt2 > (wmax1012 - dfout["flag_{}".format(i+1)])),
(dfin["flag_{}".format(i)]==2) & (wt2 < (wmin1012 - dfout["flag_{}".format(i+1)])),
(dfin["flag_{}".format(i)]==2),
(dfin["flag_{}".format(i)]==3) & (wt3 > (wmax1012 - dfout["flag_{}".format(i+1)])),
(dfin["flag_{}".format(i)]==3) & (wt3 < (wmin1012 - dfout["flag_{}".format(i+1)])),
(dfin["flag_{}".format(i)]==3)]
choices = [
(wmax1012 - dfout["flag_{}".format(i+1)]),
(wmin1012 - dfout["flag_{}".format(i+1)]),
(wt1),
(wmax1012 - dfout["flag_{}".format(i+1)]),
(wmin1012 - dfout["flag_{}".format(i+1)]),
(wt2),
(wmax1012 - dfout["flag_{}".format(i+1)]),
(wmin1012 - dfout["flag_{}".format(i+1)]),
(wt3)]
dfout["flag_{}".format(i)] = np.select(conditions, choices)
dfcum["flag_{}".format(i)] = np.select(conditions, choices)+dfcum["flag_{}".format(i+1)]
dfout=dfout.iloc[:,::-1]
dfcum=dfcum.iloc[:,::-1]
return(dfout,dfcum)
out1 = weights(flags, 0, .4, .025, .3, 0, .4)
out2 = weights(flags, 0, .4, .3, .3, 0, .4) ## This line is the problem
print(out1[0])
print(out1[1])
print(out2[0])
print(out2[1])
清楚了,谢谢。将在8分钟内时间限制到期后接受答复。
weights = weights(flags, 0, .4, .025, .3, 0, .4)
calculated_weights1 = weights(flags, 0, .4, .025, .3, 0, .4)
calculated_weights2 = weights(flags, 0, .4, .3, .3, 0, .4)
import pandas as pd
import numpy as np
flags = pd.DataFrame({'Date': ['2017-11-01','2017-12-01','2018-01-01'],
'flag_11': [2, 2, 2],
'flag_12': [2, 2, 2]})
flags = flags.set_index('Date')
print(flags)
def weights(dfin, wt1, wt2, wt3, wmin06, wmin1012, wmax1012):
dfin = pd.DataFrame(dfin)
dfout = pd.DataFrame()
dfcum = pd.DataFrame()
mapping = {1:wt1,2:wt2,3:wt3}
dfout['flag_12']=dfin['flag_12'].replace(mapping)
dfcum['flag_12']=dfin['flag_12'].replace(mapping)
i = 11
conditions = [
(dfin["flag_{}".format(i)]==1) & (wt1 > (wmax1012 - dfout["flag_{}".format(i+1)])),
(dfin["flag_{}".format(i)]==1) & (wt1 < (wmin1012 - dfout["flag_{}".format(i+1)])),
(dfin["flag_{}".format(i)]==1),
(dfin["flag_{}".format(i)]==2) & (wt2 > (wmax1012 - dfout["flag_{}".format(i+1)])),
(dfin["flag_{}".format(i)]==2) & (wt2 < (wmin1012 - dfout["flag_{}".format(i+1)])),
(dfin["flag_{}".format(i)]==2),
(dfin["flag_{}".format(i)]==3) & (wt3 > (wmax1012 - dfout["flag_{}".format(i+1)])),
(dfin["flag_{}".format(i)]==3) & (wt3 < (wmin1012 - dfout["flag_{}".format(i+1)])),
(dfin["flag_{}".format(i)]==3)]
choices = [
(wmax1012 - dfout["flag_{}".format(i+1)]),
(wmin1012 - dfout["flag_{}".format(i+1)]),
(wt1),
(wmax1012 - dfout["flag_{}".format(i+1)]),
(wmin1012 - dfout["flag_{}".format(i+1)]),
(wt2),
(wmax1012 - dfout["flag_{}".format(i+1)]),
(wmin1012 - dfout["flag_{}".format(i+1)]),
(wt3)]
dfout["flag_{}".format(i)] = np.select(conditions, choices)
dfcum["flag_{}".format(i)] = np.select(conditions, choices)+dfcum["flag_{}".format(i+1)]
dfout=dfout.iloc[:,::-1]
dfcum=dfcum.iloc[:,::-1]
return(dfout,dfcum)
out1 = weights(flags, 0, .4, .025, .3, 0, .4)
out2 = weights(flags, 0, .4, .3, .3, 0, .4) ## This line is the problem
print(out1[0])
print(out1[1])
print(out2[0])
print(out2[1])