473,378 Members | 1,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

help with loop of upper & lower letters

Thekid
145 100+
I had made a post about making a loop using letters instead of numbers and dshimer gave me this solution:
Expand|Select|Wrap|Line Numbers
  1. for i in range(65,70):
  2.     for j in range(65,70):
  3.         for k in range(65,70):
  4.             print chr(i),chr(j),chr(k)
  5.  
which used an example I had posted using numbers. That code works (thanks dshimer) but after toying with it I see that I am going about it in a method that takes a long time to run through and I also need uppers in the loop. I need a loop that will print out like this:
aaaaa
aaaaA
aaaAa
aaAaa
ans so on through every combination. I now have this portion which seems closer but still not quite there:
Expand|Select|Wrap|Line Numbers
  1. from string import letters
  2. #my 'lowers' list has extra chars at the end so I have to trim it
  3. lowers = letters[26:52]
  4. uppers = letters[:26]
  5. for lower in lowers:
  6.     for upper in uppers:
  7.         print lower,upper
  8.  
a A
a B
a C
a D

So my question is how can I get this to run through with 5 values and through every combination, without doing something like this:
Expand|Select|Wrap|Line Numbers
  1. for lower in lowers:
  2.     for upper in uppers:
  3.         print lower,lower,lower,lower,upper
  4.         print lower,lower,lower,upper,lower
  5.  
May 22 '09 #1
2 2856
bvdet
2,851 Expert Mod 2GB
Try this:
Expand|Select|Wrap|Line Numbers
  1. def permute5(a):
  2.     b = [(v,w,x,y,z) for v in a for w in a for x in a for y in a for z in a]
  3.     c = ["".join(z) for z in b]
  4.     return b,c
  5.  
  6. b, c = permute5('abcABC')
  7.  
  8. print "\n".join(c)
Partial output:
>>> aaaaa
aaaab
aaaac
aaaaA
aaaaB
aaaaC
aaaba
aaabb
aaabc
aaabA
aaabB
aaabC
aaaca
aaacb
aaacc
aaacA
aaacB
aaacC
aaaAa
..................
CCCcC
CCCAa
CCCAb
CCCAc
CCCAA
CCCAB
CCCAC
CCCBa
CCCBb
CCCBc
CCCBA
CCCBB
CCCBC
CCCCa
CCCCb
CCCCc
CCCCA
CCCCB
CCCCC
>>>
May 22 '09 #2
Thekid
145 100+
Wow....that's great! Thanks!
May 22 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: pkilambi | last post by:
I wrote this function which does the following: after readling lines from file.It splits and finds the word occurences through a hash table...for some reason this is quite slow..can some one...
8
by: ben | last post by:
i have a bit of code, that works absolutely fine as is, but seems over complicated/long winded. is there anyway to shorten/simplify it? the code is below. description of it: it's like strcpy in...
11
by: Steve Clay | last post by:
I have a small C program for a college course. It is meant to encrypt and decrypt lower case letters and leave spaces as spaces. I can't get it to run properly as I think I have a problem in the...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
1
by: vv1 | last post by:
Write a C program for reading in a message string (with no blanks) and decoding the message. Store the decoded message in another string called outString. After decoding is complete, print...
0
by: vp1 | last post by:
Write a C program for reading in a message string (with no blanks) and decoding the message. Store the decoded message in another string called outString. After decoding is complete, ...
10
by: CuTe_Engineer | last post by:
hii, i have cs assignment i tried to solve it but i still have many errors , plzz help mee :"< it`s not cheating becuz i`ve tried & wrote the prog. i just wanna you to show me my mistakes ...
3
by: majorecono | last post by:
What I'm bad at is the dialog box part. This is the code Ive come up with... if you see a way to improve what im thinkng of.... post it please. Program should.... 1. Ask user to type in a...
0
by: yabighelen | last post by:
HELP!!!!! I have to write a program that does this: Create a program that encrypts and decrypts a message... Inputs: A typed message Output: When the encrypt button is clicked the input...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.