2016-12-06 86 views
2

我嘗試將一行添加到循環內的numpy.array並且它不工作,雖然我沒有得到任何錯誤。我的總體目標是比較兩個文件並創建一個總結比較的第三個文件。將項目添加到循環中的數組

ipython

import numpy as np 

我陣列

aList1=np.array([['A','we'],['A','we'],['B','we'],['C','de']]) 
aList2=np.array([['A'],['B'],['D']]) 
aResult=np.array(['row1','occurence']) 

我的功能

def coverageA(array,file1,name1,colum1,file2,name2,colum2): 
    x=file1[1:,colum1] 
    y=file2[1:,colum2] 
    for f in x: 
     if f in y: 
      array=np.vstack((array,np.array([f,'shared']))) 
     else: 
      array=np.vstack((array,np.array([f,name1]))) 
    for f in y: 
     if f not in x: 
      array=np.vstack((array,np.array([f,name2]))) 
    return 

和使用這種方式

coverageA(aResult,alist1,'list1', 0,aList2,'list',0)

但aResult沒有改變

print(aResult) output:(['row1','occurence'])

([['row1','occurence'],['A', 'shared'],['B', 'shared'],['C','list1'],['D','list2']])

回答

1

修復:

import numpy as np 

#my arrays 

aList1=np.array([['A','we'],['A','we'],['B','we'],['C','de']]) 
aList2=np.array([['A'],['B'],['D']]) 
aResult=np.array(['row1','occurence']) 

#my function 

def coverageA(array,file1,name1,colum1,file2,name2,colum2): 
    x=file1[1:,colum1] 
    y=file2[1:,colum2] 
    for f in x: 
     if f in y: 
      array=np.vstack((array,np.array([f,'shared']))) 
     else: 
      array=np.vstack((array,np.array([f,name1]))) 
    for f in y: 
     if f not in x: 
      array=np.vstack((array,np.array([f,name2]))) 
    print(array) 
    return array 

#and use it this way 

aResult=coverageA(aResult,aList1,'list1', 0,aList2,'list2',0) 
#but aResult didn't change 

print(aResult) 
#output:(['row1','occurence']) 
#wanted 

#([['row1','occurence'],['A', 'shared'],['B', 'shared'],['C','list1'],['D','list2']]) 

解釋是,在Python參數通過賦值傳遞,這很好地解釋了here。在array=np.vstack((array,np.array([f,'shared'])))行中,新的numpy數組在一個新的內存中創建(數組指向此內存),但aResult仍指向其舊位置。您可以使用print(id(array))查看內存地址。