无法解决“inc()接受1个位置参数,但给出了2个”

2024-10-03 15:26:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试开发一个由两个自然数(不包括0)组成的元组的列表(让我们称为“l”),例如如果len(a)==len,“a”可以是“l”的memeber,对于“a”的每个成员(让我们称为p),p[0]<;=max和p[1]<;=max

例如poslist_all(max=2,len=1)

[[(1,1)],[(1,2)],[(2,1)],[(2,2)]]

和poslist_all(2,2)

^{pr2}$

所以我试着把这个列表变成迭代器,然后写了这段代码

class poslist_all:
    def __init__(self,max,len):
        self.max = max
        self.len = len
        self.iposlist = len*[(1,1)]
    def __iter__(self):
        return self
    def __next__(self):
        ml = self.maxlist()
        if ml:
            if ml[0] == 0:
                raise StopIteration
            else:
                toinc = ml[0]-1
                self.inc(ml[0] - 1)
                for i in range(ml[0],self.len-1):
                    self.iposlist[i] = (1,1)
                return self.iposlist
        else:
            self.inc(self.len - 1)
            return self.iposlist
    def maxlist(self):
        return [x for x,y in enumerate(self.iposlist) if y == (self.max,self.max)]
    def inc(pnum):
        if self.iposlist[pnum][1] == self.max:
            return (self.iposlist[pnum][0]+1,1)
        else:
            return (self.iposlist[pnum][0],self.iposlist[pnum][1]+1)

if __name__ == "__main__":
    for ps in poslist_all(2,2):
        print(ps)

但这总会回来

Traceback (most recent call last):
  File "./helper.py", line 33, in <module>
    for ps in poslist_all(2,2):
  File "./helper.py", line 22, in __next__
    self.inc(self.len - 1)
TypeError: inc() takes 1 positional argument but 2 were given

是什么导致了这个错误?如何解决? 有没有更多的Python式的方法?在


Tags: inselfforlenreturnifdefall
3条回答

类实例总是作为第一个参数传递给类的方法。尝试:

def inc(self, pnum):
    if ...:
        return ...
    else:
        return ...

更改:

def inc(pnum):
        if self.iposlist[pnum][1] == self.max:
            return (self.iposlist[pnum][0]+1,1)
        else:
            return (self.iposlist[pnum][0],self.iposlist[pnum][1]+1)

收件人:

^{pr2}$

其他人已经向您展示了如何消除错误,但我想解决实际问题。在

确实有一种更好、更像Python的方式来做你想做的事。 itertools模块,特别是{a2}可以使此任务更简单。在

import itertools as it

def create_possibilities(highest, per_list):
    tuples = it.product(range(1, highest+1), repeat=highest)
    all_possibilities = it.product(tuples, repeat=per_list)
    return all_possibilities

这将返回迭代器而不是迭代器(我认为这至少接近正确的术语)。
如果需要实际的列表,请根据需要使用list()函数。在

另外,请注意maxlen是糟糕的变量名;它们隐藏了python的内置函数。在

相关问题 更多 >