Reputation: 380
I am working on a program where I am pulling over a list from a different file to load into another file, but for some reason I cannot figure out what what to do with this array.
I usually have been writing the entire program to and from CSV files and so far have not come across this issue while trying to now integrate the values into an app.
I have the following what I thought was a list but it prints as follows:
[array([140.2]), array([160.]), array([174.]), array([156.9]), array([170.]), array([149.6]), array([160.3]), array([171.8]), array([170.7]), array([142.3]), array([147.5]), array([138.1]), array([139.5]), array([], dtype=float64), array([], dtype=float64), array([189.7]), array([134.1]), array([140.9]), array([151.]), array([167.4]), array([146.9]), array([158.6]), array([129.]), array([126.4]), array([], dtype=float64), array([143.4]), array([138.5]), array([161.8]), array([150.4]), array([126.7]), array([], dtype=float64), array([135.7]), array([138.7]), array([], dtype=float64)]
Is there a way to index this and parse as a list?
I have tried the following with no luck:
from GRABBING_TEAM_DATA_FROM_SCHEDULE import test
print(test)
test.tolist()
print(test)
Error message:
AttributeError: 'list' object has no attribute 'tolist'
So is it already technically a list then? If so is there a remove function to remove the "array" text and leave only the integers which is the only thing I want? I also tried
print(test[1])
which would work for what I want if it would return 160 but it returned 'a' from array. I create this list via Pandas and reading CSV files and using the .values
.
Upvotes: 1
Views: 1070
Reputation: 231738
Fudging a copy-n-paste, I get a list of arrays:
In [152]: array=np.array
In [154]: float64=np.float64
In [155]: alist = [array([140.2]), array([160.]), array([174.]), array([156.9]), ar
...: ray([170.]), array([149.6]), array([160.3]), array([171.8]), array([170.7
...: ]), array([142.3]), array([147.5]), array([138.1]), array([139.5]), array
...
...: ype=float64)]
In [156]: alist
Out[156]:
[array([140.2]),
array([160.]),
...
array([139.5]),
array([], dtype=float64),
array([], dtype=float64),
array([189.7]),
array([134.1]),
array([140.9]),
...
array([138.7]),
array([], dtype=float64)]
In [157]: len(alist)
Out[157]: 34
Notice that this is a mix of 1 element arrays and 0 element ones.
If we try to make array from that, we get a 'ragged' array:
In [158]: np.array(alist)
<ipython-input-158-7512d762195a>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
np.array(alist)
Out[158]:
array([array([140.2]), array([160.]), array([174.]), array([156.9]),
array([170.]), array([149.6]), array([160.3]), array([171.8]),
array([170.7]), array([142.3]), array([147.5]), array([138.1]),
array([139.5]), array([], dtype=float64), array([], dtype=float64),
array([189.7]), array([134.1]), array([140.9]), array([151.]),
array([167.4]), array([146.9]), array([158.6]), array([129.]),
array([126.4]), array([], dtype=float64), array([143.4]),
array([138.5]), array([161.8]), array([150.4]), array([126.7]),
array([], dtype=float64), array([135.7]), array([138.7]),
array([], dtype=float64)], dtype=object)
We could concatenate these arrays, though the identity of the 0d arrays disappears:
In [160]: arr = np.hstack(alist)
In [161]: arr
Out[161]:
array([140.2, 160. , 174. , 156.9, 170. , 149.6, 160.3, 171.8, 170.7,
142.3, 147.5, 138.1, 139.5, 189.7, 134.1, 140.9, 151. , 167.4,
146.9, 158.6, 129. , 126.4, 143.4, 138.5, 161.8, 150.4, 126.7,
135.7, 138.7])
So if you want to keep a list of 1 and 0 length lists, you will need to convert them individually as suggested by the other answer:
blist = [a.tolist() for a in alist]
Upvotes: 1
Reputation: 71620
Try adding the below code to your code:
test = [i.tolist() for i in test]
print(test)
Output:
[[140.2], [160.0], [174.0], [156.9], [170.0], [149.6], [160.3], [171.8], [170.7], [142.3], [147.5], [138.1], [139.5], [], [], [189.7], [134.1], [140.9], [151.0], [167.4], [146.9], [158.6], [129.0], [126.4], [], [143.4], [138.5], [161.8], [150.4], [126.7], [], [135.7], [138.7], []]
Upvotes: 1