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?
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.
Comments