How to set start and stop input in Python which generating word list

Viewed 73

I try Hard to figure out how to Input start and stop word for my Python wordlist generator.

Here is my code:

import itertools

   wrds = 'abcd0123'
   n = 5

   for i in itertools.product(wrds, repeat = n):
      a = ''.join(i)
      print(a)

My generator is giving me output like this as usual: aaaaa, aaaab, aaaac,...

But how set the inputs in order to take a starting point and an ending point?.

My length is 5. I want to generate for example from aaac1 to aaad3. How can I achieve this? I'm completely stuck.

1 Answers

Very common and beginner of doing is

import itertools

start_string=input("Enter start string: ")
end_string=input("Enter ending string: ")

wrds = 'abcd0123'
n = 5
empty_list=[]

for i in itertools.product(wrds, repeat = n):
    empty_list.append(''.join(i))

if start_string in empty_list:
    start_=empty_list.index(start_string)


if end_string in empty_list:
    end_=empty_list.index(end_string)

print(empty_list[start_:end_+1])

To have output line by line replace last print statement with below code

for item in empty_list[start_:end_+1]:
    print(item)

If you want to pass start string & ending string in random index order and if you want let code itself decide the index then you can use below code

import itertools

string_1=input("Enter start string: ")
string_2=input("Enter ending string: ")

wrds = 'abcd0123'
n = 5
empty_list=[]

for i in itertools.product(wrds, repeat = n):
    empty_list.append(''.join(i))

if string_1 in empty_list:
    index_1=empty_list.index(string_1)

if string_2 in empty_list:
    index_2=empty_list.index(string_2)

if index_1 > index_2 :
    start_ = index_2
    end_ = index_1 + 1
else :
    start_ = index_1
    end_ = index_2+1

for item in empty_list[start_:end_]:
    print(item)

To get count, you can use len() function

print("Length: ", len(empty_list[start_:end_]))

And if you are trying to find out good head start with Python basics I suggest you to read https://docs.python.org/3.6/tutorial/index.html. Its very small and can read in a single day.

Related