0

I am attempting to create a function which compares data from multiple files. The files are chosen through a dialog window (from GUI platform Tkinter)

def compare_datafiles(file_name): 
    data = np.genfromtxt(file_name, dtype=float, delimiter=',', skiprows=(2), usecols=(1,2,3,4,5,6), skip_footer=(3))
    initial = data[0,:]
    final = data[-1,:]
    weightDiff=final-initial  
    data=np.row_stack((data, weightDiff)) 
    totalWD=sum(data[4,:]) # calculate total weight
    distr=np.round((weightDiff/totalWD),2) 
    print distr
    return  distr   

for x in selectedFiles:
        M = compare_datafiles(x)

When I run the function with two input files, it prints two arrays:

runfile(r'C:\Users...)
    [ 0.23  0.04  0.1   0.14  0.12  0.38]
    [ 0.22  0.05  0.13  0.16  0.12  0.32]

But only returns one of them:

M
array([ 0.22,  0.05,  0.13,  0.16,  0.12,  0.32])

How can I get it to return both of the arrays?

4

1 回答 1

1

在您的代码中,您编写:

for x in selectedFiles:
    M = compare_datafiles(x)

M在每个循环上重新分配,因此只存储最后一个值。您可以制作M一个列表并附加到它,或使用列表推导:

>>> M = [compare_datafiles(x) for x in selectedFiles]
于 2013-10-15T08:37:35.103 回答