回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我需要读入大量的.txt文件,每个文件都包含一个十进制数(有些是正的,有些是负的),然后将它们附加到2个数组中(基因型和表型)。随后,我希望在scipy中对这些数组执行一些数学运算,但是负('-')符号会导致问题。具体来说,我无法将数组转换为float,因为“-”作为字符串读取,导致以下错误:</p>
<pre><code>ValueError: could not convert string to float:
</code></pre>
<p>这是我目前编写的代码:</p>
<pre><code>import linecache
gene_array=[]
phen_array=[]
for i in genotype:
for j in phenotype:
genotype='/path/g.txt'
phenotype='/path/p.txt'
g=linecache.getline(genotype,1)
p=linecache.getline(phenotype,1)
p=p.strip()
g=g.strip()
gene_array.<a href="https://www.cnpython.com/list/append" class="inner-link">append</a>(g)
phen_array.append(p)
gene_array=map(float,gene_array)
phen_array=map(float,phen_array)
</code></pre>
<p>在这一点上,我相当肯定是负面信号导致了这个问题,但我不清楚原因。我使用Linecache是这里的问题吗?有没有更好的替代方法?</p>
<p>结果</p>
<pre><code>print gene_array
</code></pre>
<p>是</p>
<pre><code>['-0.0448022516321286', '-0.0236187263814157', '-0.150505384829925', '-0.00338459268479522', '0.0142429109897682', '0.0286253352284279', '-0.0462358095345649', '0.0286232317578776', '-0.00747425206137217', '0.0231790239373428', '-0.00266935581919541', '0.00825077426011094', '0.0272744527203547', '0.0394829854063242', '0.0233109171715023', '0.165841084392078', '0.00259693465334536', '-0.0342590874424289', '0.0124600520095644', '0.0713627590092807', '-0.0189374898081401', '-0.00112750710611284', '-0.0161387333242288', '0.0227226505624106', '0.0382173405035751', '0.0455518646388402', '-0.0453048799717046', '0.0168570746329513']
</code></pre>