回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我有一个CSV,看起来像这样:</p>
<pre><code>0.500187550,CPU1,7.93
0.500187550,CPU2,1.62
0.500187550,CPU3,7.93
0.500187550,CPU4,1.62
1.000445359,CPU1,9.96
1.000445359,CPU2,1.61
1.000445359,CPU3,9.96
1.000445359,CPU4,1.61
1.500674877,CPU1,9.94
1.500674877,CPU2,1.61
1.500674877,CPU3,9.94
1.500674877,CPU4,1.61
</code></pre>
<p>第一列是时间,第二列是CPU使用量,第三列是能量。你知道吗</p>
<p>最后,我想要这些数组:</p>
<p>时间:</p>
<pre><code>[0.500187550, 1.000445359, 1.500674877]
</code></pre>
<p>能量(每CPU):例如CPU1</p>
<pre><code>[7.93, 9.96, 9.94]
</code></pre>
<p>对于解析我使用的CSV:</p>
<pre><code>query = csv.reader(csvfile, delimiter=',', skipinitialspace=True)
#Arrays global time and power:
for row in query:
x = row[0]
x = float(x)
x_array.append(x) #column 0 to array
y = row[2]
y = float(y)
y_array.append(y) #column 2 to array
print x_array
print y_array
</code></pre>
<p>这样我就把时间和能量的所有数据分成两个数组:<code>x_array</code>和<code>y_array</code>。你知道吗</p>
<p>然后我对数组进行排序:</p>
<pre><code>energy_core_ord_array = []
time_ord_array = []
#Dividing array into energy and time per core:
for i in range(number_cores[0]):
e = 0 + i
for j in range(len(x_array)/(int(number_cores[0]))):
time_ord = x_array[e]
time_ord_array.append(time_ord)
energy_core_ord = y_array[e]
energy_core_ord_array.append(energy_core_ord)
e = e + int(number_cores[0])
</code></pre>
<p>最后,我将时间数组剪切成它应该具有的长度:</p>
<pre><code>final_time_ord_array = []
for i in range(len(x_array)/(int(number_cores[0]))):
final_time_ord = time_ord_array[i]
final_time_ord_array.append(final_time_ord)
</code></pre>
<p>到目前为止,虽然代码并不优雅,但它仍然有效。
当我尝试获取每个核心的数组时,问题就来了。你知道吗</p>
<p>我在第一个核心中得到了它,但是当我尝试在下一个核心中迭代时,我不知道如何做到这一点,例如,如何将每个数组存储在一个具有单个名称的变量中。你知道吗</p>
<pre><code>final_energy_core_ord_array = []
#Trunk energy core array:
for i in range(len(x_array)/(int(number_cores[0]))):
final_energy_core_ord = energy_core_ord_array[i]
final_energy_core_ord_array.append(final_energy_core_ord)
</code></pre>