如何从Python中预定义的模板中列出可能的组合

2024-06-28 15:54:41 发布

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

我在做一些需要输入模板的东西。你知道吗

我需要一个生成的模板的可能性大规模

[ "Catman001","Catman002","Catman003","Catman004",....,"Catman999" ]
[ "01Catman01","01Catman02","01Catman03","01Catman04",...,"02Catman01","02Catman02"]
[ "ProAGamer","ProbGamer","ProCGamer","ProDGamer",.....,"ProZGamer"]
[ "XxGamerAxX_01","XxGamerAxX_02","XxGamerAxX_03",.....,"XxGamerBxX_01","XxGamerBxX_02",.....,"XxGamerZxX_99"]

我有很多模板,所以我希望有一个答案,可以用于上述所有模板

我试过以下方法:

Template = Catman{}
CompleteList = []

for i in range(1000):
    CompleteList.append("Template".format(Template,i))

print CompleteList

仅适用于数字。你知道吗

以及

Template = "Pro{}Gamer"

CompletedList = []

AToZ = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
atoz = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

for Alphab in AToZ:
    CompletedList.append(Template.format(Alphab))
for Alphab in atoz:
    CompletedList.append(Template.format(Alphab))

print CompletedList

只适用于一个字母。你知道吗

如果你看看我做的几个例子,它列出了所有可能的组合,我希望代码是这样的:

AToZ = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
atoz = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
Nums = ["0","1","2","3","4","5","6","7","8","9"]

Template = raw_input("Type In The Template")
Template = "XxGamer#xX_%%"

CompletedList = []

'''
# = Capital Letter
$ = Non Capital Letter
% = Numbers
'''

def TemplateCombination(Template,CompletedList):
    if ("#" in Template):
        # Adds All Possible Combinations Of Capital Letters
    if ("$" in Template):
        # Add All Possible Combinations Of Non Capital letters
    if ("%" in template):
        # Add All Possible Combinations Of Numbers
    return CompletedList

TemplateCombination(Template,CompletedList)

print CompletedList

对于那些仍然感到困惑的人,我试着列出一个列表,其中列出了一些可能的组合,例如,来自预制模板的组合

Input : "Test%Section"

应输出

OutPut : [ "Test1Section","Test2Section","Test3Section","Test4Section","Test5Section","Test6Section","Test7Section","Test8Section","Test9Section"]

我的问题是我不知道怎么做。你知道吗


Tags: in模板formatforiftemplateprintappend
1条回答
网友
1楼 · 发布于 2024-06-28 15:54:41

您可以为#$编写相应的函数。
或者可以增强扩展功能。你知道吗

def expand_nums(to_expand):
res = list()
for n in range(0,10):
    g = re.sub(r'%', str(n), to_expand, 1)
    res.append(g)

return res

res = ['asd%kk%lk%lk%zz']
while res[0].find('%') != -1:
    rres = list()
    for r in res:
        rres += expand_nums(r)
    res = rres

print(rres)

相关问题 更多 >