python - For loop, how can i use loop variable outside loop -
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(): in l1b: j in l2b: k in l3b: n = * j * k c1.append(n) in om: j in l1a: k in l2a: n = * j * k c2.append(n) def ii(): in c1: j in c2: x = (i / j) * lr rpl.append(x)
in program need loop variables i,j,k 'i' function print them in 'ii' function show combination used create x. tried 2 dimensional arrays didnt work well. there easy option work out?
the following script think trying 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) p in itertools.product(l1b, l2b, l3b)] c2 = [(reduce(operator.mul, p), p) p in itertools.product(om, l1a, l2a)] rpl = [(((p[0][0]) / p[1][0]) * lr, p[0][1], p[1][1]) p in itertools.product(c1, c2)] print rpl
this displays following type of results of 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 stored tuple result of multiplication. used when calculating rpl
value.
you format rpl
follows, show permutations made each result:
for rpl, p1, p2 in rpl: print "%8d %15s %15s" % (rpl, str(p1), str(p2))
giving output in 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 python itertools
module. provides product
function has same effect of having multiple nested for
loops. result of each iteration gives values i
, j
, k
, tuple, e.g. (27, 30, 31)
.
the reduce
command can used multiply of numbers in list returned, applying same function each entry. can't write reduce( * , p)
, can use python's operator
module provide function name version *
, i.e. operator.mul
.
the result of gets wrapped in ()
make tuple 2 parts, first result of multiplications , second part permutation produced it. e.g. (25110, (27, 30, 31))
.
c1
list holding of these value. called list comprehension. equivalent for
loop c1.append()
inside.
once c1
, c2
have been created (i suggest try , print values see like), script uses similar method calculate of rpl values. each iteration gives p
like:
((25110, (27, 30, 31)), (648, (9, 8, 9)))
this tuple 2 entries (25110, (27, 30, 31))
, (648, (9, 8, 9))
. python can access each value using indexes. 25110
use p[0][0]
first tuple, first part. or p[0][1]
(27, 30, 31)
.
the solution script converted not use list comprehensions follows:
c1 = [] p in itertools.product(l1b, l2b, l3b): multiply_all = reduce(operator.mul, p) c1.append((multiply_all, p)) c2 = [] p in itertools.product(om, l1a, l2a): multiply_all = reduce(operator.mul, p) c2.append((multiply_all, p)) rpl = [] p in itertools.product(c1, c2): calculation = (p[0][0] / p[1][0]) * lr rpl.append((calculation, p[0][1], p[1][1]))
Comments
Post a Comment