我尝试在循环中向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)
print(aResult)
output:(['row1','occurence'])
([['row1','occurence'],['A', 'shared'],['B', 'shared'],['C','list1'],['D','list2']])
答案 0 :(得分: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))
检查内存地址。