我写了一个代码,该文件需要一个txt文件并创建一个Nx3矩阵。 然后,它将删除数据超出指定条件的行。 它打印已删除的行。
我的问题是我希望它打印不符合条件的列。
我当前的代码可以吗?
我尝试使用
制作一个名为lines的新数组(lines=np.split(RD,1))
我想做3个计数检查循环,检查每列并在不满足条件的地方返回行号,但是在拆分后我不知道如何引用“行”。
def dataLoad(filename):
#Loads the txt file as an array
RD = np.loadtxt(filename)
#Rounds the numbers to one decimal
RD = np.round(RD,decimals=1)
#Defines wich are out of the conditions by setting conditions to each row.
#it then creates an array of the rows with "corrupted" data
set_to_del={row for row in range(RD.shape[0])
if (RD[row,0] < 10 or RD[row,0] > 60)
or RD[row,1] < 0
or (RD[row,2]<1 or RD[row,2]>4)}
#Deletes the rows out of conditions (The rows defined by "set_to_del")
RD = np.delete(RD,list(set_to_del),axis=0)
#Prints the lines out of condition if there are any
if (len(set_to_del)>0):
print("Følgende linjer er slettet da de er ude for parametrene(",(set_to_del),")")
#Sets data=RD so it will return the data that lives up to the conditions
data=RD
return data
print(dataLoad("test.txt"))
此代码有效,但没有显示错误发生在哪一列。