Vincent Vincent - 6 months ago 119
Python Question

Python sublist for a condition

I have 3 lists

x
,
y
,
z
and I plot them with:

ax.plot3D(x, y, z, linestyle = 'None', marker = 'o').


What is the easiest way to only plot the points where
x > 0.5
?

(my problem is how to define a sublist under a condition without making a for loop on that list).

Answer Source

It's impossible to verify a condition on every element of a list without iterating over it at least once. You could use numpy here for easy access to the elements after condition is passsed and do:

 import numpy
 x = [0.0, 0.4, 0.6, 1.0]
 y = [0.0, 2.2, 1.5, 1.6]
 z = [0.0, 9.1, 1.0, 0.9]
 res = numpy.array([[x[i], y[i], z[i]] for i in xrange(len(x)) if x[i] > 0.5])
 ax.plot3D(res[:,0], res[:,1], res[:,2], linestyle="None, marker='o'")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download