python - manipulate list comprehension -
i have 3 lists make condition, if b equal 1 want find first element in c below a, can 2 indexes subtract them , time.
= [1.373, 1.374, 1.374, 1.385, 1.385, 1.385, 1.374, 1.374] b = [0, 1, 1, 0, 0, 0, 0, 0] c = [0, 1.384, 1.385, 1.377, 0, 0, 0, 0] view = [(nx, x, nz, z, nl, l) nx, x in enumerate(a) nl, l in enumerate(b) nz, z in enumerate(c) if (l == 1) & (nz <= nx) & (z > 0) & (z <= x) & (nz == nl)]
i created simple view list shows parameters better understanding. when numbers when c lower , want first elements... expected see [(3, 1.385, 1, 1.384, 1, 1), (3, 1.385, 2, 1.385, 2, 1)]
, got that:
[(3, 1.385, 1, 1.384, 1, 1), (3, 1.385, 2, 1.385, 2, 1), (4, 1.385, 1, 1.384, 1, 1), (4, 1.385, 2, 1.385, 2, 1), (5, 1.385, 1, 1.384, 1, 1), (5, 1.385, 2, 1.385, 2, 1)] time = [(nx - nz) nx, x in enumerate(a) nl, l in enumerate(b) nz, z in enumerate(c) if (l == 1) & (nz <= nx) & (z > 0) & (z <= x) & (nz == nl)]
how can first elements when condition true?
a solve without list comprehension , @ better time complexity:
a = [1.373, 1.374, 1.374, 1.385, 1.385, 1.385, 1.374, 1.374] b = [0, 1, 1, 0, 0, 0, 0, 0] c = [0, 1.384, 1.385, 1.377, 0, 0, 0, 0] y_i, y in enumerate(b): if y == 1: x_i, x in enumerate(a): if x >= c[y_i]: # prints 0 based indexes print "iteration {}, x: {}, x index: {} c: {}, c index {}".format(y_i, x, x_i, c[y_i], y_i) break
output:
iteration 1, x: 1.385, x index: 3 c: 1.384, c index 1 iteration 2, x: 1.385, x index: 3 c: 1.385, c index 2
sometimes it;s easier start normal loops when algorithm can make better code, there no need 3 statements.
Comments
Post a Comment