writing a loop with letters not numbers

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2008
Posts: 43
Reputation: kiddo39 is an unknown quantity at this point 
Solved Threads: 0
kiddo39 kiddo39 is offline Offline
Light Poster

writing a loop with letters not numbers

 
0
  #1
May 21st, 2009
How can I write a loop using letters instead of numbers so it looks like this:
2 sets used 'a-z' and 'A-Z'
aaaaa
aaaaA
aaaAa
aaAaa
and so on:
aaaAA
then on to b:
bbbbb
bbbbA

I can do it with numbers, something like this:
  1. for i in range(10):
  2. for j in range(10):
  3. for k in range(10):
  4. print i,j,k

How does this work with letters?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 125
Reputation: slate is an unknown quantity at this point 
Solved Threads: 30
slate slate is offline Offline
Junior Poster

Re: writing a loop with letters not numbers

 
0
  #2
May 21st, 2009
You can use the magic of ascii

ord("a")==97
chr(97)=="a"
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 43
Reputation: kiddo39 is an unknown quantity at this point 
Solved Threads: 0
kiddo39 kiddo39 is offline Offline
Light Poster

Re: writing a loop with letters not numbers

 
0
  #3
May 21st, 2009
Thanks for the reply. Here's part of my problem with that:
  1. for i in range(97,122):
  2. for j in range(97,122):
  3. for k in range(97,122):
  4. print chr(i),chr(j),chr(k)
That type of loop will work but will take too long to run through the combos and I would also need the Caps (65,90) to run through it.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 125
Reputation: slate is an unknown quantity at this point 
Solved Threads: 30
slate slate is offline Offline
Junior Poster

Re: writing a loop with letters not numbers

 
0
  #4
May 21st, 2009
I can do it with numbers, something like this:
This is not true, or you did not show us that. Your code does not produce the desired output, and it is not, that it has numbers instead of characters.

I think the problem:
writing a loop with letters not numbers
is solved.

Please open a new topic with something similar:
help with simple discrete math homework.

I will gladly help.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2
Reputation: Aneftaoratos is an unknown quantity at this point 
Solved Threads: 1
Aneftaoratos Aneftaoratos is offline Offline
Newbie Poster

Re: writing a loop with letters not numbers

 
1
  #5
May 22nd, 2009
May be this would help you:
  1. def Separate (strng):
  2. uppers = []
  3. lowers = []
  4. for letter in strng:
  5. if letter.isupper():
  6. uppers.append(letter)
  7. else:
  8. lowers.append(letter)
  9.  
  10. print "Lowers:",lowers
  11. print "Uppers:",uppers

Calling the function:
  1. Separate('bbbbA')
  2. Lowers: ['b', 'b', 'b', 'b']
  3. Uppers: ['A']

1.Create two lists, one for uppers and another one for lowers

2.Use the boolean function "str.isupper()" to determine if a letter is in uppercase.

3. In case that you want to return the lowers and the uppers as string, you can use:
  1. uppersstr =''.join([i for i in uppers])
  2. lowersstr = ''.join([i for i in lowers ])
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 43
Reputation: kiddo39 is an unknown quantity at this point 
Solved Threads: 0
kiddo39 kiddo39 is offline Offline
Light Poster

Re: writing a loop with letters not numbers

 
0
  #6
May 22nd, 2009
Originally Posted by slate View Post
This is not true, or you did not show us that. Your code does not produce the desired output, and it is not, that it has numbers instead of characters.

I think the problem:

is solved.

Please open a new topic with something similar:
help with simple discrete math homework.

I will gladly help.
I put a loop up of numbers to show that I can run a loop with a given amount of chars. I can add 2 more values to it if a list of 5 is what I'm after. Running the numbers loop will produce:
0 0 0
0 0 1
0 0 2
and so on. After the suggested use of chr() I was able to write the loop to use letters instead of numbers but my original question was how to do this with uppers as well as lowers:
How can I write a loop using letters instead of numbers so it looks like this:
2 sets used 'a-z' and 'A-Z':
At your suggestion, I will mark this as solved and start a new post.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,054
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 265
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: writing a loop with letters not numbers

 
1
  #7
May 22nd, 2009
Here's some clues to help you!
  1.  
  2. >>> ord('A')
  3. 65
  4. >>> ord('a')
  5. 97
  6. >>> ord('Z')
  7. 90
  8. >>> ord('z')
  9. 122
  10. >>> from string import letters
  11. >>> letters
  12. 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  13. >>> uppers = letters[26:]
  14. >>> lowers = letters[:26]
  15. >>> uppers
  16. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  17. >>> lowers
  18. 'abcdefghijklmnopqrstuvwxyz'
  19. >>>

you cna iterate over those strings easily like this:
  1. for lower in lowers:
  2. for upper in uppers:
  3. print lower, upper
Last edited by jlm699; May 22nd, 2009 at 10:38 am.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 43
Reputation: kiddo39 is an unknown quantity at this point 
Solved Threads: 0
kiddo39 kiddo39 is offline Offline
Light Poster

Re: writing a loop with letters not numbers

 
0
  #8
May 22nd, 2009
jlm699, thank you! That helped alot!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC