For loop, how can i use loop variable outside loop

Iwko Czerniawski
LR = int(raw_input()) 
RPL = []
c1 = []
c2 = []
L1a = [8, 9, 14, 13, 12]
L2a =[9, 12, 14, 10, 8]
OM = [9, 10]
L3a = [26]
L1b = [27, 32, 26]
L2b = [30, 27, 32, 28, 31]
L3b = [31, 30, 26]

def I():
    for i in L1b:
        for j in L2b:
            for k in L3b:
                n = i * j * k
                c1.append(n)
    for i in OM:
        for j in L1a:
            for k in L2a:
                n = i * j * k
                c2.append(n)
def II():
    for i in c1:
        for j in c2:
            x = (i / j) * LR
            RPL.append(x)

In my program I need for loop variables i,j,k from 'I' function to print them in my 'II' function to show what combination was used to create x. I tried with two dimensional arrays but it didnt work well. So is there any easy option to work this out?

Martin Evans

The following script I think does what you are trying to do:

import operator, itertools

LR = int(raw_input())

L1a = [8, 9, 14, 13, 12]
L2a = [9, 12, 14, 10, 8]
L3a = [26]
L1b = [27, 32, 26]
L2b = [30, 27, 32, 28, 31]
L3b = [31, 30, 26]
OM = [9, 10]

c1 = [(reduce(operator.mul, p), p) for p in itertools.product(L1b, L2b, L3b)]
c2 = [(reduce(operator.mul, p), p) for p in itertools.product(OM, L1a, L2a)]

RPL = [(((p[0][0]) / p[1][0]) * LR, p[0][1], p[1][1])  for p in itertools.product(c1, c2)]

print RPL

This displays the following type of results of an LR of 10:

[(380, (27, 30, 31), (9, 8, 9)), (290, (27, 30, 31), (9, 8, 12)), (240, (27, 30, 31), (9, 8, 14)), ... etc

Each permutation is stored as a tuple with the result of the multiplication. This is then used when calculating your RPL value.

You could also format RPL as follows, to show which permutations made each result:

for rpl, p1, p2 in RPL:
    print "%8d  %15s  %15s" % (rpl, str(p1), str(p2))

Giving output in the form:

 380     (27, 30, 31)        (9, 8, 9)
 290     (27, 30, 31)       (9, 8, 12)
 240     (27, 30, 31)       (9, 8, 14)
 340     (27, 30, 31)       (9, 8, 10)
 430     (27, 30, 31)        (9, 8, 8)
 340     (27, 30, 31)        (9, 9, 9)

The script uses the Python itertools module. This provides a product function which has the same effect of having multiple nested for loops. The result of each iteration gives your values for i, j and k, but as a tuple, e.g. (27, 30, 31).

The reduce command can be used to multiply all of the numbers in the list that was returned, by applying the same function to each entry. As you can't write reduce( * , p), you can use Python's operator module to provide a function name version for *, i.e. operator.mul.

The result of this gets wrapped in () to make a tuple with two parts, the first is the result of the multiplications and the second part is the permutation that produced it. e.g. (25110, (27, 30, 31)).

c1 is a list holding all of these value. This is called a list comprehension. It is equivalent to a for loop with c1.append() inside.

Once c1 and c2 have been created (I suggest you try and print their values to see what they look like), the script then uses a similar method to calculate all of the RPL values. Each iteration gives p which would look like:

((25110, (27, 30, 31)), (648, (9, 8, 9)))

This is a tuple with two entries (25110, (27, 30, 31)) and (648, (9, 8, 9)). Python can access each value using indexes. So to get the 25110 you would use p[0][0] for the first tuple, first part. Or p[0][1] to get (27, 30, 31).

The solution script could be converted to not use list comprehensions as follows:

c1 = []

for p in itertools.product(L1b, L2b, L3b):
    multiply_all = reduce(operator.mul, p)
    c1.append((multiply_all, p))

c2 = []

for p in itertools.product(OM, L1a, L2a):
    multiply_all = reduce(operator.mul, p)
    c2.append((multiply_all, p))

RPL = []

for p in itertools.product(c1, c2):
    calculation = (p[0][0] / p[1][0]) * LR
    RPL.append((calculation, p[0][1], p[1][1]))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to use template variable outside a loop?

How to use the variable "rev" outside the while loop?

C# How do I use a variable outside of a loop that changes from string to int inside the loop?

I need to use variable, loop outside?

How can I use the for loop's initialization variable as a placeholder in C?

How to use tally from loop outside of that loop

How to use a php variable outside loop which is defined in foreach loop

how can i get foreach loop variable outside of loop

How do I use a variable outside the for loop in batch file?

ReactJs - i can't catch a variable outside of the axios' loop

Can I use a variable as index in for loop in python?

How can I use a loop variable in for cycle .bat

how to use a variable in a for loop outside the loop python

how can i use for loop scope variable form outside for loop?

Can I use variable in a .csv file in the for loop?

How can I use loop variable in array?

Use variable outside foreach loop

Scala : How to use variable in for loop outside loop block

how can i use the i variable outside the for loop?

To build a for loop, how can I use variable names in count() command?

How can I define a variable so it can be used outside a while loop in java?

How can I use this for loop variable in django?

How can i use a variable in a for loop in a command ? (c#)

Can we use list comprehension to work on a variable outside the for loop used?

I can't use a variable outside my loop (flutter)

How can I use a string variable in .bat for-loop?

how can i use multiple returned indices in a for loop to update a variable?

How can I use dynamic variable numbers in a loop?

how can i use an inputed variable from user in a loop and outside the loop