Consider this code:
#!/usr/bin/env python3
import numpy as np
aa = [
[3, 8, [37, 7, 5, 0, 5, 0, 8, 0]],
[3, 8, [36, 7, 5, 0, 4, 0, 8, 0]],
[3, 8, [37, 7, 5, 0, 4, 0, 8, 0]],
[3, 8, [37, 7, 5, 0, 5, 0, 9, 0]],
[3, 8, [36, 7, 6, 0, 6, 0, 12, 0]],
[3, 8, [36, 7, 5, 0, 5, 0, 9, 0]],
[3, 8, [36, 7, 5, 0, 5, 0, 8, 0]],
[3, 8, [37, 7, 6, 0, 6, 0, 10, 0]],
[3, 8, [37, 7, 6, 0, 6, 0, 10, 0]],
[3, 8, [37, 7, 6, 0, 6, 0, 12, 0]]
]
nch = np.asarray(aa, dtype=object)
print("nch shape {}".format(nch.shape))
print(nch)
nchB = nch[:,2]
print("nchB shape {}".format(nchB.shape))
print(nchB)
print("Test 1")
print( np.frompyfunc(list, 0, 1)(np.empty((3,2), dtype=object)) )
print("Test 2")
print( np.frompyfunc(list, 0, 1)(nchB) )
print("Test 3")
print( np.frompyfunc(list, 1, 1)( nchB ) )
It outputs:
nch shape (10, 3)
[[3 8 list([37, 7, 5, 0, 5, 0, 8, 0])]
[3 8 list([36, 7, 5, 0, 4, 0, 8, 0])]
[3 8 list([37, 7, 5, 0, 4, 0, 8, 0])]
[3 8 list([37, 7, 5, 0, 5, 0, 9, 0])]
[3 8 list([36, 7, 6, 0, 6, 0, 12, 0])]
[3 8 list([36, 7, 5, 0, 5, 0, 9, 0])]
[3 8 list([36, 7, 5, 0, 5, 0, 8, 0])]
[3 8 list([37, 7, 6, 0, 6, 0, 10, 0])]
[3 8 list([37, 7, 6, 0, 6, 0, 10, 0])]
[3 8 list([37, 7, 6, 0, 6, 0, 12, 0])]]
nchB shape (10,)
[list([37, 7, 5, 0, 5, 0, 8, 0]) list([36, 7, 5, 0, 4, 0, 8, 0])
list([37, 7, 5, 0, 4, 0, 8, 0]) list([37, 7, 5, 0, 5, 0, 9, 0])
list([36, 7, 6, 0, 6, 0, 12, 0]) list([36, 7, 5, 0, 5, 0, 9, 0])
list([36, 7, 5, 0, 5, 0, 8, 0]) list([37, 7, 6, 0, 6, 0, 10, 0])
list([37, 7, 6, 0, 6, 0, 10, 0]) list([37, 7, 6, 0, 6, 0, 12, 0])]
Test 1
[[list([]) list([])]
[list([]) list([])]
[list([]) list([])]]
Test 2
[list([]) list([]) list([]) list([]) list([]) list([]) list([]) list([])
list([]) list([])]
Test 3
[list([]) list([]) list([]) list([]) list([]) list([]) list([]) list([])
list([]) list([])]
Basically, I use something like nchB
to feed a matplotlib boxplot, which works fine.
nchB
here is considered to be a single dimension array of length 10, with its elements being lists; it so happens here, each of these lists has 8 elements.
Now, I would want to create an array, which is also a a single dimension array of length 10, with its elements being lists; except I'd want each list to have only one or two elements. So I would want to obtain, say:
[list([37, 7]) list([36, 7])
list([37, 7]) list([37, 7])
list([36, 7]) list([36, 7])
list([36, 7]) list([37, 7])
list([37, 7]) list([37, 7])]
or:
[list([37]) list([36])
list([37]) list([37])
list([36]) list([36])
list([36]) list([37])
list([37]) list([37])]
... somehow from nchB
, preferably by using a one-liner - then I could use this "reduced" array of lists to feed maxplotlib's boxplot data for initialization (so I can start setting up the plot, and not have to wait a lot of time for my actual data to be rendered).
How can I do this? Obviously, the trivial attempts I made in "Test 2" and "Test 3" above with np.frompyfunc
, which I found from:
... don't quite work, as all I get are empty lists.