1

I have a problem in Python I simply can't wrap my head around, even though it's fairly simple (I think).

I'm trying to make "string series". I don't really know what it's called, but it goes like this:

I want a function that makes strings that run in series, so that every time the functions get called it "counts" up once.

I have a list with "a-z0-9._-" (a to z, 0 to 9, dot, underscore, dash). And the first string I should receive from my method is aaaa, next time I call it, it should return aaab, next time aaac etc. until I reach ----

Also the length of the string is fixed for the script, but should be fairly easy to change.

(Before you look at my code, I would like to apologize if my code doesn't adhere to conventions; I started coding Python some days ago so I'm still a noob).

What I've got:

Generating my list of available characters

chars = []
for i in range(26):
    chars.append(str(chr(i + 97)))

for i in range(10):
    chars.append(str(i))

chars.append('.')
chars.append('_')
chars.append('-')

Getting the next string in the sequence

iterationCount = 0
nameLen = 3
charCounter = 1
def getString():
    global charCounter, iterationCount
    name = ''
    for i in range(nameLen):
        name += chars[((charCounter + (iterationCount % (nameLen - i) )) % len(chars))]
    charCounter += 1
    iterationCount += 1
    return name

And it's the getString() function that needs to be fixed, specifically the way name gets build.

I have this feeling that it's possible by using the right "modulu hack" in the index, but I can't make it work as intended!

4
  • 2
    Look up how to create and use a Python Generator for this. Commented Apr 25, 2013 at 21:02
  • That's definately fix my iteration problem. But the problem is how to actually generate the strings :) Commented Apr 25, 2013 at 21:06
  • @mata beat me too it. I was going to say you will be amazed at how easy the solution will be. Commented Apr 25, 2013 at 21:13
  • doens't make you less cool, thanks for trying at least :) Commented Apr 25, 2013 at 21:38

1 Answer 1

8

What you try to do can be done very easily using generators and itertools.product:

import itertools

def getString(length=4, characters='abcdefghijklmnopqrstuvwxyz0123456789._-'):
    for s in itertools.product(characters, repeat=length):
        yield ''.join(s)

for s in getString():
    print(s)


aaaa
aaab
aaac
aaad
aaae
aaaf
...    
Sign up to request clarification or add additional context in comments.

4 Comments

Since it is a generator, it will automatically stop when it gets to the end of the itertools.product sequence.
Would be better to call len - length so as to not shadow the builtin, and itertools.product(*([characters]*len)) seems a bit convoluted - I would suggest itertools.product(characters, repeat=length)
@JonClements - you're right :) pydoc itertools.product doesn't mention the repeat keyword argument (at least not right on top)
As they write in the tutorial; "Batteries included". This was the exact answer I was looking for :) Thanks mata! That was awesome.. Once again SO is a dazzeling magical place for knowledge.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.