Kiwi
Kiwi

Reputation: 83

python dynamically create loops

I have a 2D Matrix named table and a list named count. In table the data is stored in count the numer of datasets in each column. first_index just show the number of the combination in this case there are 588 combinations (7*6*2*7) Now i want to create a any to any relation. My code is static so i need a possibility to create dynamic loops/Variables.

table:

[1, 30, 50, 60]
[2, 31, 51, 61]
[3, 32, 0, 62]
[4, 33, 0, 63]
[5, 34, 0, 64]
[6, 35, 0, 65]
[7, 0, 0, 66]

count:

[7, 6, 2, 7]

The Code works fine in my case but it is not sure if there are more than 4 rows so it is not really good code. I am a noob in python maybe there is another way to solve this problem

for k in range(count[0]):
    for kk in range(count[1]):
        for kkk in range(count[2]):
            for kkkk in range(count[3]):
                print('{0:3} , {1:3} , {2:1}'.format(first_index, table[k][0], 1))
                print( '{0:3} , {1:3} , {2:1}'.format(first_index, table[kk][1], 2))
                print( '{0:3} , {1:3} , {2:1}'.format(first_index, table[kkk][2], 3))
                print( '{0:3} , {1:3} , {2:1}'.format(first_index, table[kkkk][3], 4))
                print
                first_index+=1

the output look like

1 ,   1 , 1
1 ,  30 , 2
1 ,  50 , 3
1 ,  60 , 4

2 ,   1 , 1
2 ,  30 , 2
2 ,  50 , 3
2 ,  61 , 4

...

588 ,   7 , 1
588 ,  35 , 2
588 ,  51 , 3
588 ,  66 , 4

Upvotes: 3

Views: 2589

Answers (2)

Inbar Rose
Inbar Rose

Reputation: 43467

Here is using the itertools.product but using clever logic.

from itertools import product

def special_combinations(table):
    for r in product(*zip(*table)):
        if 0 in r:
            continue
        yield r

You do not need the count variable at all. Using this solution:

>>> table = [[1, 30, 50, 60],
             [2, 31, 51, 61],
             [3, 32,  0, 62],
             [4, 33,  0, 63],
             [5, 34,  0, 64],
             [6, 35,  0, 65],
             [7,  0,  0, 66]]
>>> for idx, val in enumerate(special_combinations(table)):
    print idx+1, val

1 (1, 30, 50, 60)
2 (1, 30, 50, 61)
3 (1, 30, 50, 62)
4 (1, 30, 50, 63)
5 (1, 30, 50, 64)
6 (1, 30, 50, 65)
7 (1, 30, 50, 66)
8 (1, 30, 51, 60)
9 (1, 30, 51, 61)
10 (1, 30, 51, 62)
...
584 (7, 35, 51, 62)
585 (7, 35, 51, 63)
586 (7, 35, 51, 64)
587 (7, 35, 51, 65)
588 (7, 35, 51, 66)

Bonus: A one-liner:

[(i+1, R) for i, R in enumerate(r for r in product(*zip(*table)) if not 0 in r)]

Note: You could get much better performance if you trimmed the zeroes out of your table.

>>> table
[[1, 30, 50, 60], 
[2, 31, 51, 61], 
[3, 32, 0, 62], 
[4, 33, 0, 63], 
[5, 34, 0, 64], 
[6, 35, 0, 65], 
[7, 0, 0, 66]]
>>> table = [t[:t.index(0)] if 0 in t else t for t in map(list, zip(*table))]
>>> table
[[1, 2, 3, 4, 5, 6, 7], 
[30, 31, 32, 33, 34, 35], 
[50, 51], 
[60, 61, 62, 63, 64, 65, 66]]

And then your solution is much simpler.

>>> [(i+1, R) for i, R in enumerate(r for r in product(*table))]

Upvotes: 4

Saullo G. P. Castro
Saullo G. P. Castro

Reputation: 58955

Here if follows the solution using itertools.product:

from itertools import product
first_index=1
for i in product(*[range(i) for i in count]):
    for j in range(len(count)):
        print( '{0:3} , {1:3} , {2:1}'.format(first_index, table[i[j]][j], j+1))
    first_index += 1

Upvotes: 1

Related Questions