Manipulating a list of numbers into columns or separate lists for construction in Python
I'm new to Python, so I'm sorry if it's pretty simple. (it seems easy to me, but I'm afraid ...)
I have a list of numbers dropped from a Keithley SMU 2400 from an IV sweep I did, and the list of results is ordered like below:
[v0, c0, t0, v1, c1, t1, v2, c2, t2, ...]
The list is a list of numbers (not a list of ascii values thanks to the PyVisa query_ascii_values command).
How can I parse them in any column of numbers (for output in csv or similar) or three separate lists for plotting in matplotlib.
The result that I liked will end up looking like:
volts = [v0, v1, v2, v3...]
currents = [c0, c1, c2, c3...]
times = [t0, t1, t2, t3...]
which should make it easier to plot in matplotlib (or output to a csv text file).
Note that my v0, v1, etc. are just my names to them, they are numbers at the present time.
I would try to do it in Matlab, similar to this:
volts = mydata(1:3:length(mydata));
(calling the index by counting every third element of 1)
Thanks for your thoughts and help! Also, are there any good resources for simply collecting data in a way that I should get a copy?
source to share
If you want to keep it in the list, then Divakar's solution works well. However, if you do parsing and plotting later, you really want to use a numpy array, and that makes whatever you want to do even easier.
To get it into a numpy array, you can do:
>>> import numpy as np
>>>
>>> mydata = np.array(mydata)
To get it in separate variables, you can simply do:
>>> volts, currents, times = mydata.reshape(-1,3).T
This converts to an Nx3 array, then transfers it to a 3xN array, and then puts each line in a separate variable. One of the advantages of this is that it is very fast, as only one array is ever created, as opposed to the list approach in which 4 lists have to be created (especially if you put the data directly into a numpy array).
You can also use the identical Divakar approach used with lists, again avoiding creating additional lists.
Having said that, I highly recommend that you take a look at pandas. This will make it easier to track the data. You can label the columns of an array with meaningful names, which means you don't need to split the data into three variables to keep track of it.
source to share