Python 将数字分解为其他数字

Python 将数字分解为其他数字,python,python-3.x,string,list,split,Python,Python 3.x,String,List,Split,我正在尝试编写一段代码,它将返回一个大数字(最多3位数)的可能分解 数字由num=str([0-999])+str([0-999])+str([0-999])构成。所有组件都是独立的和随机的 例如,'1111'的预期输出将是:[[1,11,1],[11,1,1],[1,1,11] 到目前为止,我编写的代码是: def isValidDecomp(num,decmp,left): if(len(num)-len(decmp)>=left): if(int(decmp)

我正在尝试编写一段代码,它将返回一个大数字(最多3位数)的可能分解
数字由
num=str([0-999])+str([0-999])+str([0-999])
构成。所有组件都是独立的和随机的
例如,
'1111'
的预期输出将是:
[[1,11,1],[11,1,1],[1,1,11]

到目前为止,我编写的代码是:

def isValidDecomp(num,decmp,left):
    if(len(num)-len(decmp)>=left):
        if(int(decmp)<999):
            return True
    return False

def decomp(num,length,pos,left):
    """
    Get string repping the rgb values
    """
    while (pos+length>len(num)):
        length-=1
    if(isValidDecomp(num,num[pos:pos+length],left)):
        return(int(num[pos:pos+length])) 
    return 0 #additive inverse

def getDecomps(num):
    length=len(num)
    decomps=[[],[],[]]
    l=1
    left=2
    for i in range(length): 
        for j in range(3):
            if(l<=3):
                decomps[j].append(decomp(num,l,i,left))
                l+=1
            if(l>3): #check this immediately
                left-=1
                l=1#reset to one
    return decomps

d=getDecomps('11111')

print( d)

有人能告诉我我做错了什么吗?

如果我正确理解了这个问题,可以通过调整找到的方法来实现这一点,该方法返回所有可能的分割输入字符串的方法:

def splitter(s):
    for i in range(1, len(s)):
        start = s[0:i]
        end = s[i:]
        yield (start, end)
        for split in splitter(end):
            result = [start]
            result.extend(split)
            yield tuple(result)
然后过滤来自生成器的结果:

def getDecomps(s):
    return [x for x in splitter(s) if len(x) == 3 and all(len(y) <= 3 for y in x)]
def getDecomps(s):
    return [x for x in splitter(s) if len(x) == 3 and all(len(y) <= 3 for y in x)]
>>> getDecomps('1111')
[('1', '1', '11'), ('1', '11', '1'), ('11', '1', '1')]
>>> getDecomps('111')
[('1', '1', '1')]
>>> getDecomps('123123145')
[('123', '123', '145')]
>>> getDecomps('11111')
[('1', '1', '111'),
 ('1', '11', '11'),
 ('1', '111', '1'),
 ('11', '1', '11'),
 ('11', '11', '1'),
 ('111', '1', '1')]