[英]How to generate a range of strings from aa… to zz
I am trying to create a Python program where it accepts a string and displays permutations based on that. 我正在尝试创建一个Python程序,在该程序中它接受一个字符串并显示基于该字符串的排列。
For example. 例如。
If the user enters "[AAA] Grim" it should generate words starting from "aaa Grim" then "aab Grim" then "aac Grim" to "zzz Grim" etc. Or if the user enter "[AA] Grim" it should generate "aa Grim", "ab Grim", "ac Grim" to "zz Grim" 如果用户输入“ [AAA] Grim”,则应生成从“ aaa Grim”,“ aab Grim”,“ aac Grim”到“ zzz Grim”等的单词。或者,如果用户输入“ [AA] Grim”,则应生成“ aa Grim”,“ ab Grim”,“ ac Grim”为“ zz Grim”
Sorry for the unclear question title. 抱歉,标题标题不清楚。 I'm not sure on how to word what I require without an example.
我不确定如果没有示例,该如何措辞。 :)
:)
If I understood what you meant the following will do: 如果我了解您的意思,则可以执行以下操作:
import re
import string
import itertools
user_in = input("Enter string: ")
p_len = user_in.index(']') - user_in.index('[')
text = user_in[user_in.index(']')+1:]
print('\n'.join([ ''.join(p) + text for p in itertools.combinations_with_replacement(string.ascii_lowercase,r=p_len-1)]))
Will produce (an extract): 将产生(提取物):
Enter string: [AA] Grim
aa Grim
ab Grim
ac Grim
ad Grim
ae Grim
input = '[AAA] Grim'
n = len(input.split(' ')[0]) - 2
s = input[n+2:]
a = [''.join(x)+s for x in itertools.product(string.ascii_lowercase, repeat=n)]
>>> import string
>>> ltrs = string.lowercase
>>> [''.join([a,b]) for a in ltrs for b in ltrs]
The code is from here: https://mail.python.org/pipermail/tutor/2005-July/040117.html 代码来自这里: https : //mail.python.org/pipermail/tutor/2005-July/040117.html
I found it and it works great! 我找到了,效果很好!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.