943,644 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1361
  • Python RSS
May 21st, 2009
0

writing a loop with letters not numbers

Expand Post »
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:
Python Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
kiddo39 is offline Offline
50 posts
since Nov 2008
May 21st, 2009
0

Re: writing a loop with letters not numbers

You can use the magic of ascii

ord("a")==97
chr(97)=="a"
Reputation Points: 56
Solved Threads: 65
Posting Whiz in Training
slate is offline Offline
242 posts
since Jun 2008
May 21st, 2009
0

Re: writing a loop with letters not numbers

Thanks for the reply. Here's part of my problem with that:
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
kiddo39 is offline Offline
50 posts
since Nov 2008
May 21st, 2009
0

Re: writing a loop with letters not numbers

Quote ...
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:
Quote ...
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.
Reputation Points: 56
Solved Threads: 65
Posting Whiz in Training
slate is offline Offline
242 posts
since Jun 2008
May 22nd, 2009
1

Re: writing a loop with letters not numbers

May be this would help you:
python Syntax (Toggle Plain Text)
  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:
python Syntax (Toggle Plain Text)
  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:
python Syntax (Toggle Plain Text)
  1. uppersstr =''.join([i for i in uppers])
  2. lowersstr = ''.join([i for i in lowers ])
Reputation Points: 11
Solved Threads: 1
Newbie Poster
Aneftaoratos is offline Offline
2 posts
since Jul 2008
May 22nd, 2009
0

Re: writing a loop with letters not numbers

Click to Expand / Collapse  Quote originally posted by slate ...
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:
Quote ...
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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
kiddo39 is offline Offline
50 posts
since Nov 2008
May 22nd, 2009
1

Re: writing a loop with letters not numbers

Here's some clues to help you!
python Syntax (Toggle Plain Text)
  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:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
May 22nd, 2009
0

Re: writing a loop with letters not numbers

jlm699, thank you! That helped alot!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
kiddo39 is offline Offline
50 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: How to include loops in independent python programs
Next Thread in Python Forum Timeline: Fun Questions...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC