简体   繁体   English

Python:返回循环结果时出错

[英]Python: error in returning loop results

My output for the following code is only save the last row of result instead of putting every single row of values into the csv file. 我的以下代码输出仅保存结果的最后一行,而不是将每一行值都放入csv文件中。 I have limited knowledge in python. 我对python的了解有限。 I think my looping part is incorrect. 我认为我的循环部分不正确。 Can anyone help me? 谁能帮我?

Code

import numpy as np
from numpy import genfromtxt

with open('binary.csv') as actg:
    actg=actg.readlines()
    with open('single.csv') as single:
        single=single.readlines()
        with open('division.csv') as division:
            division=division.readlines()

            for line in actg:
                for line2 in single:
                    for line1 in division:

                        myarray = np.fromstring(line, dtype=float, sep=',')
                        myarray = myarray.reshape((-1, 3, 4))
                        a=np.asmatrix(myarray)
                        a=np.array(a)

                        single1 = np.fromstring(line2, dtype=float, sep=',')
                        single1 = single1.reshape((-1, 4))
                        s=np.asmatrix(single1)
                        s=np.array(s)

                        division1 = np.fromstring(line1, dtype=float, sep=',')
                        m=np.asmatrix(division1)
                        m=np.array(m)
                        res2 = (s[np.newaxis,:,:] / m[:,np.newaxis,:] * a).sum(axis=-1)

np.savetxt("output.csv", res2, delimiter=",")

binary.csv 二进制文件

0,1,0,0,1,0,0,0,0,0,0,1
0,0,1,0,1,0,0,0,1,0,0,0

single.csv: single.csv:

0.28,0.22,0.23,0.27,0.12,0.29,0.34,0.21,0.44,0.56,0.51,0.65

division.csv 部门

0.4,0.5,0.7,0.1
0.2,0.8,0.9,0.3

Expected output 预期产量

 0.44,0.3,6.5
 0.26,0.6,2.2

Actual output 实际产量

0.26,0.6,2.2

It is not you python understanding. 不是您对python的了解。 It is your loop logic understanding. 这是您对循环逻辑的理解。

If you look closely at your loops, you (and not Python) always keep the last elements. 如果仔细观察循环, (而不是Python)将始终保留最后一个元素。

In the first and second loops you overwrite each time the variable that you need and you do not keep the overall information for each iteration. 在第一个和第二个循环中,您每次都会覆盖所需的变量,并且不会保留每次迭代的总体信息。

In the last loop, you declare the row variable as new list in each iteration. 在最后一个循环中,您在每次迭代中将row变量声明为新列表。

Also, the i variable is not set up anywhere except as the index variable in the for loop! 另外,除了在for循环中作为索引变量外, i变量没有在任何地方设置!

Because of the fact that you only write out the results after the loops have finished, you are only seeing the last result. 由于仅在循环完成后才写出结果,因此您只能看到最后一个结果。
Also, I would expect 4 results from that data, not 2. 另外,我希望从该数据得到4个结果,而不是2个。
You need to write the output on each iteration or store each iteration and then write out the stored values. 您需要在每次迭代中写入输出或存储每次迭代,然后写出存储的值。
Try printing res2 each time you calculate it and you will see something like this: 每次计算时尝试打印res2 ,您会看到类似以下内容:

('Final Results', array([[ 0.44,  0.3 ,  6.5 ]]))
('Final Results', array([[ 0.275     ,  0.6       ,  2.16666667]]))
('Final Results', array([[ 0.32857143,  0.3       ,  1.1       ]]))
('Final Results', array([[ 0.25555556,  0.6       ,  2.2       ]]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM