Python List Conversion Help


#1

I hope someone can assist here. Working on some plugin updates for the bed level visualizer and can't wrap my head around converting a list of 3D points to a 2D list needed for the rendering. It has to be able to work with different x/y values and an unknown length, but if given the following list of 3D points.

a = [[40,20,7.31],[80,20,7.38],[120,20,7.35],[120,70,7.37],[80,70,7.41],[40,70,7.35],[40,120,7.33],[80,120,7.36],[120,120,7.34]]

I need to convert it to a format that include a list of x values, a list of y values and a list of z values that match up with the order of x and y. So the above needs to end up as the following.

x = [40,80,120]
y = [20,70,120]
z = [[7.31,7.38,7.35],[7.35,7.41,7.37],[7.33,7.36,7.34]]

From everything I'm seeing doing searches for this type of operation, it seems numpy is the most commonly used library to convert lists, but can't seem to find this exact use case. Was hoping some of you more savvy python programmers could shed some light into a good way to do this.


#2

So I think I kludged my way through it using numpy with the following code. I'm sure there is a more efficient method to achieve this, please speak up if you have a better idea.

import numpy as np
a = np.swapaxes(self.mesh,1,0)
x = np.unique(a[0]).astype(np.float)
y = np.unique(a[1]).astype(np.float)
z = a[2].reshape((len(x),len(y)))
self.mesh = np.subtract(z, [self.old_marlin_offset], dtype=np.float, casting='unsafe').tolist()