缺少参数错误,但给出了一个

发布于 2025-01-12 06:11:58 字数 1052 浏览 3 评论 0原文

我有一个无监督学习课程,其中一个功能似乎无缘无故地抛出错误。我抛出错误的类方法是:

def flatten(self,cluster):
    out = []
    for item in cluster:
        if isinstance(item, (list, tuple)):
            out.extend(HC.flatten(item))
        else:
            out.append(item)
    return out

这是我在网上找到的一个函数,它的参数是一个列表并将其展平,作为参数输入的列表的输出直到列表似乎抛出了错误以及所述错误:

[Running] python -u "c:\Users\benti\Group Project 2\HC\Complete Linkage.py"
(51, 98)
(343, 390)
(150, 317)
(7, 209)
(173, 235)
(11, 85)
((343, 390), 82)
Traceback (most recent call last):
  File "c:\Users\benti\Group Project 2\HC\Complete Linkage.py", line 88, in <module>
    Z= HC.run2()
  File "c:\Users\benti\Group Project 2\HC\Complete Linkage.py", line 82, in run2
    cluster2 = HC.flatten(cluster2)          
  File "c:\Users\benti\Group Project 2\HC\Super.py", line 170, in flatten
    out.extend(HC.flatten(item))
TypeError: flatten() missing 1 required positional argument: 'cluster'

[Done] exited with code=1 in 8.745 seconds

我应该注意我正在使用超类并且没有在该类中抛出此错误。

I have a unsuperivsed learning class and one of the functions is throwing an error for seemingly no reason. My class method that's throwing the error is:

def flatten(self,cluster):
    out = []
    for item in cluster:
        if isinstance(item, (list, tuple)):
            out.extend(HC.flatten(item))
        else:
            out.append(item)
    return out

Which is a function I have found online,its aruement is a list and it flattens it, the output of the lists that are being entered as the argument up to the list which seems to be throwing the error along with the stated error:

[Running] python -u "c:\Users\benti\Group Project 2\HC\Complete Linkage.py"
(51, 98)
(343, 390)
(150, 317)
(7, 209)
(173, 235)
(11, 85)
((343, 390), 82)
Traceback (most recent call last):
  File "c:\Users\benti\Group Project 2\HC\Complete Linkage.py", line 88, in <module>
    Z= HC.run2()
  File "c:\Users\benti\Group Project 2\HC\Complete Linkage.py", line 82, in run2
    cluster2 = HC.flatten(cluster2)          
  File "c:\Users\benti\Group Project 2\HC\Super.py", line 170, in flatten
    out.extend(HC.flatten(item))
TypeError: flatten() missing 1 required positional argument: 'cluster'

[Done] exited with code=1 in 8.745 seconds

I should note I'm using a superclass and am not getting this error thrown within that class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时间海 2025-01-19 06:11:58

看起来您想要一个静态方法,请从方法签名中删除 self 参数

class HC:
    def flatten(cluster): # Only cluster remains
        out = []
        for item in cluster:
            if isinstance(item, (list, tuple)):
                out.extend(HC.flatten(item))
            else:
                out.append(item)
        return out

flatten_test = ((343, 390), 82)
print(HC.flatten(toflatten)) # [343, 390, 82]

Looks like you want a static method, remove the self argument from the method signature

class HC:
    def flatten(cluster): # Only cluster remains
        out = []
        for item in cluster:
            if isinstance(item, (list, tuple)):
                out.extend(HC.flatten(item))
            else:
                out.append(item)
        return out

flatten_test = ((343, 390), 82)
print(HC.flatten(toflatten)) # [343, 390, 82]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文