python - Simpler Way of Creating Lists from other Lists -
i have 3 lists: x, y, , z. same size, need create new lists of x, y, , z, depending on value @ each index of z, shown below:
xnew = [] ynew = [] znew = [] = 0 value in z: if value > 0: xnew.append(x[i]) ynew.append(y[i]) znew.append(z[i]) += 1 does know if there tidier, perhaps more efficient, way of performing above computation?
probably straightforward way:
it = zip(x, y, z) xnew, ynew, znew = zip(*(t t in if t[-1] > 0)) we use the zip function twice restore original structure of data.
zip(x, y, z) creates new iterator object, yields triples.
(t t in if t[-1] > 0) filters triples (t[-1] value).
zip(*(...)) yields 3 tuples , xnew, ynew , znew receive them.
Comments
Post a Comment