How do you iteratre over a list performing some operation that turns a single element into multiple?
<p>I have a csv file of data from a LiDAR sensor that looks like this, but with a bagillion more lines:</p>

<pre><code>scan_0,scan_1,scan_2
timestamp_0,timestamp_1,timestamp_2
7900200,7900225,7900250
logTime_0,logTime_1,logTime_2
27:46.8,27:46.8,27:46.8
distance_0,distance_0,distance_0
132,141,139
136,141,155
139,141,155
138,143,155
138,143,142
139,143,136
138,143,136
</code></pre>

<p>This is data from a planar sensor. So Scan_0 is a list or "radial" coordinates for the specific scan at a time stamp. </p>

<p>My plan is to:</p>

<ol>
<li>Read the CSV file into a list</li>
<li>Transpose the list</li>
<li><p>Turn each element of row into a xyz format like the example below.</p>

<pre><code>scan_0 -----&gt; scan_0
timestamp_0-&gt; timestamp_0
7900200-----&gt; 7900200
logTime_0---&gt; logTime_0
27:46.8-----&gt; 27:46.8
distance_0--&gt; distance_0
132---------&gt; [132*cos(1),132*sin(1),7900200]
136---------&gt; [136*cos(2),136*sin(2),7900200]
139---------&gt; [139*cos(3),139*sin(3),7900200]
138---------&gt;          .          .
138---------&gt;          .          .
139---------&gt;          .          .
138---------&gt; [138*cos(7),139*sin(7),7900200]
</code></pre></li>
<li><p>Write the array of xyz coordinates to a new csv file with one coordinate per line'</p></li>
<li><p>eventually use a trajectory instead of timestamp for the z coordinate from another csv file.</p></li>
</ol>

<p>I tell you all of this, so you have some context for my motivation. </p>

<p>here is what I have so far:</p>

<pre><code>import csv 
import numpy
from numpy import genfromtxt
from numpy import vsplit
import math

with open('2016_09_16_14_29_09_953.csv',"r") as f:
    reader = csv.reader(f,delimiter = ",")
    data = list(reader)
    row_count = len(data)
print row_count
with open("out.csv", "a") as f1:
    my_r = genfromtxt('2016_09_16_14_29_09_953.csv', delimiter=',', skip_header=6, autostrip=True) #import data
    my_r = my_r.T #transpose
    for x in my_r:
        i=0
        while i &lt; row_count/360:
            x = [x*math.cos(i), x*math.sin(i), i]
            i = i + row_count/360

    thedatawriter = csv.writer(f1) #setup writer
    for row in my_r: #write the data
        thedatawriter.writerow(row)
</code></pre>

<p>All this really does it ouput the transpose of the source file. It appears that I am having trouble turning the single entry in the list into a list itself. What am I doing wrong here? Any help, advice, and guidance is greatly appreciated.  </p>

<p>The problem that you have is that you are trying to assign new value to variable that does not reference to array.</p>

<p>So you have </p>

<pre><code>for x in my_r:
    i=0
    while i &lt; row_count/360:
        x = [x*math.cos(i), x*math.sin(i), i]
        i = i + row_count/360
</code></pre>

<p>You cannot do that, because x is not actual element of <code>my_r</code> list(changing <code>x</code> would not lead to changing element on <code>my_r</code>).</p>

<p>The simplest way is to create new array to store desired values.</p>

<pre><code>my_r = genfromtxt('2016_09_16_14_29_09_953.csv', delimiter=',', skip_header=6, autostrip=True) #import data
my_r = my_r.T #transpose
new_r = []
for x in my_r:
    i=0
    while i &lt; row_count/360:
        new_r.append([x*math.cos(i), x*math.sin(i), i])
        i = i + row_count/360
thedatawriter = csv.writer(f1) #setup writer
for row in new_r: #write the data
    thedatawriter.writerow(row)
</code></pre>

python