Normally a pure list approach is faster when starting with small lists. But since you are feeding a numpy
function, which probably will apply asarray
to its inputs, I'd suggest an array transpose approach.
arr = np.array(pts) # 2x3x2 array
arr = arr.reshape(6,2)
x, y = arr.T # unpack a 2x6 array
Testing:
In [614]: pts
Out[614]:
[[(120, 1200), (121, 1201), (122, 1202)],
[(130, 1300), (131, 1301), (132, 1302)]]
In [615]: np.array(pts).shape
Out[615]: (2, 3, 2)
In [616]: np.array(pts).reshape(-1,2).T
Out[616]:
array([[ 120, 121, 122, 130, 131, 132],
[1200, 1201, 1202, 1300, 1301, 1302]])
In [617]: y, x = np.array(pts).reshape(-1,2).T
In [618]: y
Out[618]: array([120, 121, 122, 130, 131, 132])
In [619]: x
Out[619]: array([1200, 1201, 1202, 1300, 1301, 1302])
np.polyfit
starts with:
order = int(deg) + 1
x = NX.asarray(x) + 0.0
y = NX.asarray(y) + 0.0
If pts
had been created with extend
the common zip*
would have been enough
In [625]: pts = []
...: y = [120, 121, 122]
...: x = [1200, 1201, 1202]
...: pts.extend(list(zip(y, x)))
...: x = [1300, 1301, 1302]
...: y = [130, 131, 132]
...: pts.extend(list(zip(y, x)))
...:
In [626]: pts
Out[626]: [(120, 1200), (121, 1201), (122, 1202), (130, 1300), (131, 1301), (132, 1302)]
In [627]: y,x = list(zip(*pts))
In [628]: y
Out[628]: (120, 121, 122, 130, 131, 132)
In [629]: x
Out[629]: (1200, 1201, 1202, 1300, 1301, 1302)
The chain
flattening can be combined with the *zip
transpose, eliminating the need for any list comprehension.
In [642]: pts
Out[642]:
[[(120, 1200), (121, 1201), (122, 1202)],
[(130, 1300), (131, 1301), (132, 1302)]]
In [643]: y,x=list(zip(*chain(*pts)))
In [644]: y
Out[644]: (120, 121, 122, 130, 131, 132)
In [645]: x
Out[645]: (1200, 1201, 1202, 1300, 1301, 1302)