You can achieve this with Python's default package itertools.product.
import itertools
chars = "0123456789abcdefghijklmnopqrstuvwxyz"
n = 2
for i in xrange(1, n+1):
for item in itertools.product(chars, repeat=i):
print "".join(item)
Where n is the max number of characters.
The output will look like this.
0
1
2
...
y
z
00
01
02
...
Answer from Jaakko on Stack OverflowYou can achieve this with Python's default package itertools.product.
import itertools
chars = "0123456789abcdefghijklmnopqrstuvwxyz"
n = 2
for i in xrange(1, n+1):
for item in itertools.product(chars, repeat=i):
print "".join(item)
Where n is the max number of characters.
The output will look like this.
0
1
2
...
y
z
00
01
02
...
You have a program that creates all length n strings if given a list of all length n-1 strings.
You currently use it to generate all length-2 strings from a list of the length-1 strings (data).
So you just need to make it a function, and call it for each of the remaining lengths you need, in order, passing it the strings of length 1 less.
The only (slight) complication is that you don't separate strings of different lengths, but that should be easy to handle.
Sequence[str] - is this solution crazy?
Question about Sequence[str]
total stranger to python and dealing with invalid escape sequence...
PyLint: Error code for "invalid escape sequence" ?
str is a Sequence[str] in Python -- a common footgun.
Here's the laziest solution I've found to this in my own projects. I want to know if it's too insane to introduce at work:
-
Have ruff require the following import:
from useful_types import SequenceNotStr as Sequence
2. ...that's it.
You could avoid the useful_types dependency by writing the same SequenceNotStr protocol in your own module.
I plan to build up on this solution by writing a pre commit hook to allow this import to be unused (append the #noqa: F401 comment).
EDIT: https://github.com/python/typing/issues/256 for context if people don't know this issue.