Hey all. I have this program where I need a variable number of for loops. For eg. the program i use to find the pythogorean triplets is

for n1 in range(100):
    for n2 in range(100):
        for n3 in range(100):
            if n1**2+n2**2 == n3**2:
                print str(n1**2) + '+' + str(n2**2) + '=' + str(n3**2)

Now if I needed n number of for loops (I will also need their vars) how do I dot it ?

Also, please dont give a solution which works only for this.

Thanks.

Edited 4 Years Ago by athulram

I do not think you should include 0 as possible value to your triplets. And of course there is more efficient methods than brute force.

>>>print(list((a,b,c) for a,b,c in itertools.product(range(1, 100), repeat=3) if a<=b<=c and a**2 + b**2 == c**2))
[(3, 4, 5), (5, 12, 13), (6, 8, 10), (7, 24, 25), (8, 15, 17), (9, 12, 15), (9, 40, 41), (10, 24, 26), (11, 60, 61), (12, 16, 20), (12, 35, 37), (13, 84, 85), (14, 48, 50), (15, 20, 25), (15, 36, 39), (16, 30, 34), (16, 63, 65), (18, 24, 30), (18, 80, 82), (20, 21, 29), (20, 48, 52), (21, 28, 35), (21, 72, 75), (24, 32, 40), (24, 45, 51), (24, 70, 74), (25, 60, 65), (27, 36, 45), (28, 45, 53), (30, 40, 50), (30, 72, 78), (32, 60, 68), (33, 44, 55), (33, 56, 65), (35, 84, 91), (36, 48, 60), (36, 77, 85), (39, 52, 65), (39, 80, 89), (40, 42, 58), (40, 75, 85), (42, 56, 70), (45, 60, 75), (48, 55, 73), (48, 64, 80), (51, 68, 85), (54, 72, 90), (57, 76, 95), (60, 63, 87), (65, 72, 97)]

Edited 4 Years Ago by pyTony

Even in this snippet there are 3 only varialbes I will need n number of variables.

You can rewrite PyTony's snippet with only 1 variable x

>>> print(list(x for x in itertools.product(range(1, 100), repeat=3) if x[0]<=x[1]<=x[2] and x[0]**2 + x[1]**2 == x[2]**2))

Another nice tool is generators

def orduples(size, start, stop, step=1):
    if size == 0:
        yield ()
    elif size == 1:
        for x in range(start, stop, step):
            yield (x,)
    else:
        for u in orduples(size - 1, start, stop, step):
            for x in range(u[-1], stop, step):
                yield u + (x,)

if __name__ == "__main__":
    print(list(orduples(3, 0, 5)))

""" my output -->
[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 0, 4), (0, 1, 1), (0, 1, 2),
(0, 1, 3), (0, 1, 4), (0, 2, 2), (0, 2, 3), (0, 2, 4), (0, 3, 3), (0, 3, 4),
(0, 4, 4), (1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 2, 2), (1, 2, 3),
(1, 2, 4), (1, 3, 3), (1, 3, 4), (1, 4, 4), (2, 2, 2), (2, 2, 3), (2, 2, 4),
(2, 3, 3), (2, 3, 4), (2, 4, 4), (3, 3, 3), (3, 3, 4), (3, 4, 4), (4, 4, 4)]
"""

Edited 4 Years Ago by Gribouillis

I dont think I've explained the my needs properly. I would like to actually check each value of one var with its parent. So if n2 should be tested from 1 to 500 with 1 of n1.

I dont think I've explained the my needs properly. I would like to actually check each value of one var with its parent. So if n2 should be tested from 1 to 500 with 1 of n1.

This does not at all explain your needs. I understand that you want to test a certain property for a certain set of values. There must be a way to describe more precisely this set of values as well as the property.

Edited 4 Years Ago by Gribouillis

I'll try my best. Imaging we're making a game where the computer has to guess a word. And it needs to use brute force. So if the word is "hey".
I would use 3 for loops each with its own var as an alphabet so in the parent loop it would test 'A' with A-Z of the second loop and in the third loop it would test 'AA' with A-Z.

word = 'hey'
for n1 in range(1, 129):
    char = chr(n1)
    if(char == word):
        print 'word is' + char
    for n2 in range(1, 129):
        char2 = chr(n1)
        if(char+char2 == word):
            print 'word is' + char1 + char2
        for n3 in range(1, 129):
            char3 = chr(n1)
            if(char+char2+char3 == word):
                print 'word is' + char1 + char2 + char3

I know the code is crude I just put it up fast.
For a 3 letter word this word suffice but for word which has n number of alphabets what would you do ?

Edited 4 Years Ago by athulram

As tony said, simply use itertools.product

import itertools

def brute(items, n):
    """generate tuples of len 1 to n with items chosen in a given sequence"""
    items = list(items)
    for k in range(1, n+1):
        for t in itertools.product(items, repeat = k):
            yield t

for t in brute(range(97, 100), 3):
    word = ''.join(chr(x) for x in t)
    print(word)

Edited 4 Years Ago by Gribouillis

This question has already been answered. Start a new discussion instead.