help with loop of upper & lower letters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Thekid
    New Member
    • Feb 2007
    • 145

    help with loop of upper & lower letters

    I had made a post about making a loop using letters instead of numbers and dshimer gave me this solution:
    Code:
    for i in range(65,70):
        for j in range(65,70):
            for k in range(65,70):
                print chr(i),chr(j),chr(k)
    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:
    Code:
    from string import letters
    #my 'lowers' list has extra chars at the end so I have to trim it
    lowers = letters[26:52]
    uppers = letters[:26]
    for lower in lowers:
        for upper in uppers:
            print lower,upper
    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:
    Code:
    for lower in lowers:
        for upper in uppers:
            print lower,lower,lower,lower,upper
            print lower,lower,lower,upper,lower
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Try this:
    Code:
    def permute5(a):
        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]
        c = ["".join(z) for z in b]
        return b,c
    
    b, c = permute5('abcABC')
    
    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
    >>>

    Comment

    • Thekid
      New Member
      • Feb 2007
      • 145

      #3
      Wow....that's great! Thanks!

      Comment

      Working...