help iterating over list of strings to get my desired output

This is the place for queries that don't fit in any of the other categories.

help iterating over list of strings to get my desired output

Postby pizzaboy150 » Thu Aug 01, 2013 10:09 pm

I approached this project expecting it to be simple but failed from the out set I want a function to print this...

a
b
c
d
e
etc...
aa
ab
ac
ad
ae
etc...
ba
bb
bc
bd
be

Its suprisingly hard or am I just a bit dim... :?
Last edited by Yoriz on Thu Aug 01, 2013 11:40 pm, edited 1 time in total.
Reason: changed title
pizzaboy150
 
Posts: 11
Joined: Sun Feb 24, 2013 11:14 pm

Re: extreme looping

Postby micseydel » Thu Aug 01, 2013 10:10 pm

Code?
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 941
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: extreme looping

Postby pizzaboy150 » Thu Aug 01, 2013 10:13 pm

thats my problem

i have this

Code: Select all
alpha = ['a','b','c','d','e','f','g','h','i','j',
             'k','l','m','n','o','p','q','r','s','t',
             'u','v','w','x','y','z','A','B','C','D',
             'E','F','G','H','I','J','K','L','M','N',
             'O','P','Q','R','S','T','U','V','W','X',
             'Y','Z','1','2','3','4','5','6','7','8',
             '9','0']

for item in alpha:
    print(item)



but im not sure how to extend
pizzaboy150
 
Posts: 11
Joined: Sun Feb 24, 2013 11:14 pm

Re: extreme looping

Postby micseydel » Thu Aug 01, 2013 10:18 pm

You can shorten the beginning with
Code: Select all
from string import letters, digits
ALPHA = letters + digits

Also, you've literally made no code attempt to create the output you've posted. Make some effort, and ask as specific of questions as you can.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 941
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: extreme looping

Postby pizzaboy150 » Thu Aug 01, 2013 10:18 pm

Code: Select all
    alpha = ['a','b','c','d','e','f','g','h','i','j',
             'k','l','m','n','o','p','q','r','s','t',
             'u','v','w','x','y','z','A','B','C','D',
             'E','F','G','H','I','J','K','L','M','N',
             'O','P','Q','R','S','T','U','V','W','X',
             'Y','Z','1','2','3','4','5','6','7','8',
             '9','0']
   
    for item in alpha:

        current = item

        for second in alpha:

            print(current + second)


This works as I want but I have to add a for loop for each character and if I want it to continue for 50 characters then do I really need to write 50 for loops?
pizzaboy150
 
Posts: 11
Joined: Sun Feb 24, 2013 11:14 pm

Re: extreme looping

Postby pizzaboy150 » Thu Aug 01, 2013 10:44 pm

Code: Select all
    alpha = ['a','b','c','d','e','f','g','h','i','j',
             'k','l','m','n','o','p','q','r','s','t',
             'u','v','w','x','y','z']
   
    content = ['','','','']   
   
    for first in alpha:
        content[0] = first
       
        for second in alpha:
            content[1] = second
           
            for third in alpha:
                content[2] = third
               
                for fourth in alpha:
                    content[3] = fourth

                    print(content[0] + content[1] + content[2] + content[3])
                    if (content[0] + content[1] + content[2] + content[3]) == 'abel':
                        print('\n\n' + content[0] + content[1] + content[2] + content[3])
                        return


this code runs better and stops when it finds 'able' but its running backwords and is dependent on me having enough for loops for the word length?
pizzaboy150
 
Posts: 11
Joined: Sun Feb 24, 2013 11:14 pm

Re: extreme looping

Postby micseydel » Thu Aug 01, 2013 11:12 pm

If you're searching for a word with a know length, you can use itertools.product().
Code: Select all
from itertools import product
for chars in product(ALPHA, repeat=length):
    word = ''.join(chars)
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 941
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: help iterating over list of strings to get my desired ou

Postby pizzaboy150 » Fri Aug 02, 2013 3:10 pm

I was not aware of the itertools module so that may help further

the example show worked well but it ran backwards from right to left and was dependent on me knowing how many characters the word is.

I have managed to have it run the other way by reversing my for loops which works but is still dependent on me knowing the length of the word.

Code: Select all
def start():
   
    alpha = ['a','b','c','d','e','f','g','h','i','j',
                 'k','l','m','n','o','p','q','r','s','t',
                 'u','v','w','x','y','z']

    content = ['','','','']

    for first in alpha:
            content[3] = first
           
            for second in alpha:
                content[2] = second
               
                for third in alpha:
                    content[1] = third
                   
                    for fourth in alpha:
                        content[0] = fourth

                        print(content[0] + content[1] + content[2] + content[3])
                        if (content[0] + content[1] + content[2] + content[3]) == 'abel':
                            print('\n\n' + content[0] + content[1] + content[2] + content[3])
                            return

if __name__ == '__main__':
    start()
pizzaboy150
 
Posts: 11
Joined: Sun Feb 24, 2013 11:14 pm

Re: help iterating over list of strings to get my desired ou

Postby pizzaboy150 » Fri Aug 02, 2013 3:21 pm

BINGO!!!

Code: Select all
from itertools import product

def start():
   
    alpha = ['a','b','c','d','e','f','g','h','i','j',
                 'k','l','m','n','o','p','q','r','s','t',
                 'u','v','w','x','y','z']

    length=1
    success = False

    while success == False:

        for chars in product(alpha, repeat=length):
            word = ''.join(chars)
            print(word)

        length += 1
       
if __name__ == '__main__':
    start()


Thanks micseydel itertools had the answer this code works and there is less of it!!!
pizzaboy150
 
Posts: 11
Joined: Sun Feb 24, 2013 11:14 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 1 guest

cron