Python 将父前缀编号展开为给定的子编号

Python 将父前缀编号展开为给定的子编号,python,Python,我有一个父前缀编号和一个以相同前缀开头的子编号。我想将父编号扩展到目标子编号 例如,以一种包括所有前缀并显示目标的方式 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + Example #1 + Example #2

我有一个父前缀编号和一个以相同前缀开头的子编号。我想将父编号扩展到目标子编号 例如,以一种包括所有前缀并显示目标的方式

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                                                         +
+ Example #1              +  Example #2               +  Example #3                +
+ parent=123, target=1235 +  parent=123, target=12354 +  parent=123, target=123073 +
+                         +                           +                            +
+ Expansion would be:     +  Expansion would be:      +  Expansion would be:       +
+                         +                           +                            +
+ 123-0                   +  123-0                    +  123-0-0                   +
+ 123-1                   +  123-1                    +  123-0-1                   +
+ 123-2                   +  123-2                    +  123-0-2                   +
+ 123-4                   +  123-4                    +  123-0-3                   +
+ 123-5  --> target       +  123-5-0                  +  123-0-4                   +
+ 123-6                   +  123-5-1                  +  123-0-5                   +
+ 123-7                   +  123-5-2                  +  123-0-6                   +
+ 123-8                   +  123-5-4  --> target      +  123-0-7-0                 +
+ 123-9                   +  123-5-5                  +  123-0-7-1                 +
+                         +  123-5-6                  +  123-0-7-2                 +
+                         +  123-5-7                  +  123-0-7-3 --> target      +
+                         +  123-5-8                  +  123-0-7-4                 +
+                         +  123-5-9                  +  123-0-7-5                 +
+                         +  123-6                    +  123-0-7-6                 +
+                         +  123-7                    +  123-0-7-7                 +
+                         +  123-8                    +  123-0-7-8                 +
+                         +  123-9                    +  123-0-7-9                 +
+                         +                           +  123-0-8                   +
+                         +                           +  123-0-9                   +
+                         +                           +  123-1                     +
+                         +                           +  123-2                     +
+                         +                           +  123-3                     +
+                         +                           +  123-4                     +
+                         +                           +  123-5                     +
+                         +                           +  123-6                     +
+                         +                           +  123-7                     +
+                         +                           +  123-8                     +
+                         +                           +  123-9                     +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
如你所见

  • 在示例#1中,目标长度为4位,并且只有一级扩展 需要
  • 在示例#2中,目标为5位长度和两个级别 需要扩大
  • 在示例#3中,目标是6位长度和三级扩展 需要
在下面显示的当前代码中,我只能打印一个级别的扩展,并且只能打印一个目标。当目标长度比父项的长度大2位数或更多,并且输入目标不止一个时,我陷入了如何扩展父项编号的困境。我们将不胜感激

parent="1312314"
child="13123147674"

l = len(parent)
target=child[l:]

t=int(target[0])

for k in range(t):
    print(parent + str(k))

print(parent + str(t) + " --> target")

for k in range(t+1,10):
    print(parent + str(k))
电流输出:

13123140
13123141
13123142
13123143
13123144
13123145
13123146
13123147 --> target
13123148
13123149
更新

Example #4
parent=12345, target1=1234538, target2=1234570924, target3=123459

Expansion would be:

123450
123451
123452
1234530
1234531
1234532
1234533
1234534
1234535
1234536
1234537
1234538 --> target
1234539
123454
123455
123456
12345700
12345701
12345702
12345703
12345704
12345705
12345706
12345707
12345708
123457090
123457091
1234570920
1234570921
1234570922
1234570923
1234570924 --> target
1234570925
1234570926
1234570927
1234570928
1234570929
123457093
123457094
123457095
123457096
123457097
123457098
123457099
1234571
1234572
1234573
1234574
1234575
1234576
1234577
1234578
1234579
123458
123459 --> target
你可以这样做:

parent='123' 
child='123073'

if len(child)> len(parent):
    tuple_list =[]

    nums_to_append=child[len(parent):]
    l = len(nums_to_append)

    for i,num in enumerate(nums_to_append):
        for j in range(10):
            if num == str(j):
                tuple_list.append((parent,j+1))
                parent = parent+num
                if i==(l-1):
                    print(parent, "--> target")

                break
            else:
                print(parent+str(j))

    for parent,num in tuple_list[::-1]:
        for j in range(num,10):
            print(parent+str(j))


你可以试试这个。诀窍是
result.sort()

结果:

1230
1231
1232
1233
1234
1235
12350
12351
12352
12353
12354--> target
12355
12356
12357
12358
12359
1236
1237
1238
1239
对于多个目标:

parent="12345"
target1="1234538"
target2="1234570924"
target3="123459"

targets = [target1, target2, target3]
result = []

for target in targets:
    for i in range(len(parent), len(target)):
        for x in range(0, 10):
            y = target[0:i] + str(x)
            result.append(y)    

result = list(set(result))
result.sort()

for a in result:
    print (a + ("--> target" if a in targets else ""))

因为您在目标[0]中仅获取1个元素。所以是7。如果需要其他元素,则需要循环target[i]。在如何循环target[i]这一部分中,我一直在研究如何使其工作。如果要使其无限大(例如,第二个数字比第一个数字长),则我认为这需要一个递归函数。幸运的是,只有当第二个数字比第一个数字长2个字符时,这才有效one@Shijith你好谢谢你的帮助。我一直在尝试,它似乎工作得很好。我想知道当有多个目标(1到n个目标)时,您是否可以帮助如何使其工作。我用示例4更新了我的原始帖子,其中有3个目标和相应的输出。谢谢你的帮助。它工作非常出色,甚至是一个更紧凑的解决方案。如果你有机会回答我原始帖子中的第二个问题,请你帮助我,那就是处理目标不止一个的情况,如例4所示。再次感谢你可以用目标列表来包装它。我更新了我的答案
1230
1231
1232
1233
1234
1235
12350
12351
12352
12353
12354--> target
12355
12356
12357
12358
12359
1236
1237
1238
1239
parent="12345"
target1="1234538"
target2="1234570924"
target3="123459"

targets = [target1, target2, target3]
result = []

for target in targets:
    for i in range(len(parent), len(target)):
        for x in range(0, 10):
            y = target[0:i] + str(x)
            result.append(y)    

result = list(set(result))
result.sort()

for a in result:
    print (a + ("--> target" if a in targets else ""))