我有一個(gè)代碼,我想將其放入 for 循環(huán)中。我想將一些存儲(chǔ)為文件的數(shù)據(jù)輸入到我的代碼中,并根據(jù)每個(gè)輸入自動(dòng)生成輸出。目前,我的代碼僅適用于一個(gè)輸入文件,因此給出一個(gè)輸出。我的輸入文件名為model000.msh,但事實(shí)是我有一系列這些輸入文件model000.msh,其名稱(chēng)model001.msh為 、 等。在代碼中,我對(duì)導(dǎo)入的文件進(jìn)行一些計(jì)算,最后將其與my_data另一個(gè)具有ID一列和數(shù)千行的 numpy 數(shù)組 () 生成的 numpy 數(shù)組 () 進(jìn)行比較。IDarray 是我要迭代的第二個(gè)變量。ID正在my_data通過(guò)一個(gè)np.concatenate函數(shù)進(jìn)行。我想用 的每一列ID來(lái)制作my_data( my_data=np.concatenate((ID[:,iterator], gr), axis =1))。因此,我想迭代多個(gè)文件,然后從每個(gè)文件中提取數(shù)組(extracted),然后按照循環(huán)my_data從 的每一列生成ID并進(jìn)行計(jì)算my_data,extracted最后使用動(dòng)態(tài)命名方法導(dǎo)出每次迭代的結(jié)果(changed_000,changed_001等等在)。這是我的一個(gè)單一輸入和一個(gè)單一my_data數(shù)組的代碼(由ID只有一列的數(shù)組組成),但我想更改對(duì)多個(gè)輸入文件和多個(gè)my_data數(shù)組以及最后幾個(gè)輸出的迭代:from itertools import islicewith open('model000.msh') as lines: nodes = np.genfromtxt(islice(lines, 0, 1000))with open('model000.msh', "r") as f: saved_lines = np.array([line.split() for line in f if len(line.split()) == 9])saved_lines[saved_lines == ''] = 0.0elem = saved_lines.astype(np.int)# following lines extract some data from my fileextracted=np.c_[elem[:,:-4], nodes[elem[:,-4]-1, 1:], nodes[elem[:,-3]-1, 1:],nodes[elem[:,-2]-1, 1:], nodes[elem[:,-1]-1, 1:]]…extracted =np.concatenate((extracted, avs), axis =1) # each input file ('model000.msh') will make this numpy array# another data set, stored as a numpy array is compared to the data extracted from the fileID= np.array [[… ..., …, …]] # now, it is has one column, but it should have several columns and each iteration, one column will make a my_data arraymy_data=np.concatenate((ID, gr), axis =1) # I think it should be something like my_data=np.concatenate((ID[:,iterator], gr), axis =1)from scipy.spatial import distancedistances=distance.cdist(extracted [:,17:20],my_data[:,1:4])ind_min_dis=np.argmin(distances, axis=1).reshape(-1,1)我感謝您提前提供的任何幫助。
1 回答

婷婷同學(xué)_
TA貢獻(xiàn)1844條經(jīng)驗(yàn) 獲得超8個(gè)贊
我不太確定你的問(wèn)題。但你似乎在要求類(lèi)似的東西:
for idx in range(10):
? ? with open('changed_{:0>2d}'.format(idx), 'a') as fout:
? ? ? ? with open('model0{:0>2d}.msh'.format(idx), 'r') as fin:
? ? ? ? ? ? #read something from fin...
? ? ? ? ? ? #calculate something...
? ? ? ? ? ? #write something to fout...
添加回答
舉報(bào)
0/150
提交
取消