简体   繁体   English

如何生成从aa…到zz的字符串范围

[英]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.

相关问题 如何在这个问题中迭代。实际上我发现从 A 到 Z 我们会收到索引 1-26,从 AA 到 ZZ 是 26+26^2。但是如何进行? - HOW TO ITERATE IN THIS QUESTION.ACTUALLY I FOUND THAT FROM A to Z WE WOULD RECIEVE INDEX 1-26 AND FROM AA TO ZZ IT IS AS 26+26^2.BUT HOW TO PROCEED? 如何在python中编写字母bigram(aa,ab,bc,cd…zz)频率分析计数器? - How to write a alphabet bigram (aa, ab, bc, cd … zz) frequency analysis counter in python? 生成范围内的连续字符串 - Generate continuous character strings in a range 如何从 INPUT 生成范围内的随机数? - How to generate random numbers in range from INPUT? 获取 Excel 列 label (A, B, ..., Z, AA, ..., AZ, BA, ..., ZZ, AAA, AAB, ...) - Get the Excel column label (A, B, ..., Z, AA, ..., AZ, BA, ..., ZZ, AAA, AAB, ...) 用于匹配 XX/YY 字符串但不匹配 XX/YY/ZZ 字符串的正则表达式 - Regex for matching XX/YY strings but not XX/YY/ZZ strings 如何产生交替范围? - How to generate an alternating range? 如何从 Plotly.py 的图例中删除“Aa”? - How to remove 'Aa' from the legend in Plotly.py? 如何为每个月(在某个时间范围内)自动生成日期时间字符串来抓取网站? - How can I automatically generate datetime strings for each month (between a certain time range) to scrape a website? 如何从 2 个字符串生成唯一键? - How can I generate a unique key from 2 strings?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM