I have a numpy list of arrays (a two-dimensional list):
db = [ch1, ch2, ch3, ch4, ch5, ch6, ch7, ch8, ch9, ch10, ch11, ch12, ch13, ch14, ch15, ch16]
I would like to perform some operations in these arrays like this:
for i in db:
newch = (eegFilter(i)/sens)+25
How can I create a new two-dimensional list with the results of each loop iteration so that the new array would look something like this:
[[newch_iteration_1], [newch_iteration_2], [newch_iteration_3], ....]
asked Feb 25, 2014 at 5:51
-
1Lists and arrays are very different things. A list of arrays is not a two-dimensional list, and neither a list of arrays nor a 2D list is what you should be using, which is a 2D array. If you're using NumPy, you want to avoid using lists to hold or work with numbers. What are you actually using to store your data?user2357112– user23571122014年02月25日 06:03:16 +00:00Commented Feb 25, 2014 at 6:03
-
thank you for the clarification. the python script reads the data from a csv file and then stores it in a 2D list to manipulate it. is this not the most efficient way. the data files are usually very big (hundreds of megs)Carlos Muñiz– Carlos Muñiz2014年02月26日 12:03:52 +00:00Commented Feb 26, 2014 at 12:03
1 Answer 1
Use a list comprehension:
[((eegFilter(i)/sens)+25).reshape(1, *i.shape) for i in db]
Demo:
In [12]: db = [np.arange(10).reshape(2, 5), np.arange(12).reshape(3, 4)]
In [13]: [(x%2).reshape(1, *x.shape) for x in db]
Out[13]:
[array([[[0, 1, 0, 1, 0],
[1, 0, 1, 0, 1]]]),
array([[[0, 1, 0, 1],
[0, 1, 0, 1],
[0, 1, 0, 1]]])]
answered Feb 25, 2014 at 5:55
Comments
lang-py