使用变长python函数参数时出现迭代问题
如果我运行上面的代码,它会显示:使用变长python函数参数时出现迭代问题,python,python-3.x,function,variables,args,Python,Python 3.x,Function,Variables,Args,如果我运行上面的代码,它会显示: def function1(self,*windows): xxx_per_win = [[] for _ in windows] for i in range(max(windows),self.file.shape[0]): for j in range(len(windows)): zz = self.file['temp'][i-windows[j]:i].quantile(0.25)
def function1(self,*windows):
xxx_per_win = [[] for _ in windows]
for i in range(max(windows),self.file.shape[0]):
for j in range(len(windows)):
zz = self.file['temp'][i-windows[j]:i].quantile(0.25)
...
...
...
o = classX(file)
windows = [3,10,20,40,80]
output = o.function1(windows)
以及:
只有当窗口长度可变时(即*窗口而不仅仅是窗口),才会出现此问题
我该如何解决这个问题?是什么导致了这种情况?函数希望传递多个参数,而不是包含多个参数的元组。您的
windows
变量是一个元组
因此,不是:
zz = self.file['temp'][i-windows[j]:i].quantile(0.25)
TypeError: unsupported operand type(s) for -: 'int' and 'list'
改为这样做:
for i in range(max(windows),self.file.shape[0]):
关于第二个错误,涉及行:
for i in range(max(*windows),self.file.shape[0]):
好的,你在减法,它抱怨你不能从整数中减去一个列表。因为我不知道windows[j]包含什么,所以我不能说里面是否有列表。。但如果有,就不可能有。你还没有给我们一个有效的例子来尝试
我建议您在代码中添加一些调试输出,例如:
zz = self.file['temp'][i-windows[j]:i].quantile(0.25)
# TypeError: unsupported operand type(s) for -: 'int' and 'list'
然后查看数据的外观。函数希望传递多个参数,而不是包含多个参数的元组。您的
windows
变量是一个元组
因此,不是:
zz = self.file['temp'][i-windows[j]:i].quantile(0.25)
TypeError: unsupported operand type(s) for -: 'int' and 'list'
改为这样做:
for i in range(max(windows),self.file.shape[0]):
关于第二个错误,涉及行:
for i in range(max(*windows),self.file.shape[0]):
好的,你在减法,它抱怨你不能从整数中减去一个列表。因为我不知道windows[j]包含什么,所以我不能说里面是否有列表。。但如果有,就不可能有。你还没有给我们一个有效的例子来尝试
我建议您在代码中添加一些调试输出,例如:
zz = self.file['temp'][i-windows[j]:i].quantile(0.25)
# TypeError: unsupported operand type(s) for -: 'int' and 'list'
这样就可以看到您的数据是什么样子。当您只运行函数定义时,不会出现错误,您能否提供一个调用函数以抛出错误的最小示例代码?问题显然在于未显示的输入数据的格式。如何调用该方法?
windows
变量的一些示例值是什么?@azro更好?@tadhgmdonald Jensen更好?仅运行函数定义时不会出现错误,能否提供调用函数以抛出错误的最小示例代码?很明显,问题在于未显示的输入数据的格式。如何调用该方法?windows
变量的一些示例值是什么?@azro更好吗?@tadhgmandald Jensen更好吗?这解决了第一个问题。抱歉,最初忘记将第二个(相关)问题放在问题中,您将向函数传递什么?这修复了第一个问题。抱歉,最初忘记将第二个(相关)问题放在问题中,您将向函数传递什么?