簡體   English   中英

如何使用列表的元素之一來調出特定的數據列表

[英]How to use one of the elements of the list to call out a specific data list

我有 4000 個原子,我有 5 個不同的時間框架:對於每個時間框架,每個原子都有 4000 組 XY 和 Z 坐標。 我正在 python 中編寫代碼以從 output 文件中讀取數據。 我得到了要在列表中調用的坐標,如何操作時間范圍,以便在調用特定時間范圍內的特定原子時調用,而不是調用其他時間范圍的 rest 中的數據。 謝謝你的幫助。 確定:這是一個示例:t = 0 原子數 = 4000 0.0 16.795961913825074 0.0 16.795961913825074 0.0 16.795961913825074

項目:原子 id 類型 xyz vx vy vz fx fy fz

[1.0, 1.0, 0.0, 0.0, 0.0, 1.1087, 0.233604, 0.0980598, -6.4837e-14, -6.26166e-14, -6.25611e-14] [2.0, 1.0, 0.839798, 0.839798, 4,0.0,4,0.0,4 , -1.30119, 9.32587e-15, 1.11855e-14, -6.19504e-14]...

專注於 x,y,z。 其他 4 個時間范圍內也有類似的數據。 目標是根據 id、坐標(x、y 和 z 分別)調用原子,並能夠在給定的時間范圍內 select。 總而言之: x[id][x or y or z][t] 應該在正確的時間范圍內輸出該原子 id 的坐標。

這是我的代碼:

使用 open('/Users/marx/Documents/Research/PL_Files/lata4olivia', 'r') 作為閱讀器:line = reader.readline()

# Read and print the entire file line by line
x = []
while line != '':
    
    if line.rstrip() == "ITEM: TIMESTEP":
        line = reader.readline()
        t = int (line)
        print ('t =', t) 
        line = reader.readline()

    if line.rstrip() == "ITEM: NUMBER OF ATOMS":
        line = reader.readline()        
        na = int (line)
        print ('Number of Atoms =', na)

        line = reader.readline()        
    if line.rstrip() == "ITEM: BOX BOUNDS pp pp pp":
        line = reader.readline()
        line = line.split(' ')
        xlo = float(line[0])
        xhi = float(line[1])
        print (xlo,xhi)
        line = reader.readline()
        line = line.split(' ')
        ylo = float(line[0])
        yhi = float(line[1])
        print(ylo,yhi)
        line = reader.readline()
        line = line.split(' ')
        zlo = float(line[0])
        zhi = float(line[1])
        print(zlo,zhi)

        line = reader.readline()  
    if line.rstrip() == "ITEM: ATOMS id type x y z vx vy vz fx fy fz":
        for i in range (1,na):
            line = reader.readline()        
            outcomes = line
            outcomes = line.rstrip('\n')
            outcomes = line.split(' ')
            outcomes = [float(ele) for ele in outcomes]
            iid,itype,ix,iy,iz,ivx,ivy,ivz,ifx,ify,ifz = list(outcomes)
            print (outcomes)
            x.append([iid,ix,iy,iz])
    #print(x)


            
    line = reader.readline()

編輯-既然你已經給出了一個例子,我建議你像這樣存儲數據:

data = {
    time_frame: {
        atom_id: {
            'x': value_of_x,
            'y': value_of_y,
            'z': value_of_z,
        }
    }
}
time = 0
atom_id = 1
# get x of atom 1 in time frame 0
data[time][atom_id][x]
>>> value_of_x

我相信,如果您共享您的代碼,它會更容易提供幫助 - 但這是一種存儲數據並使其易於訪問且易於操作的方法(如果我理解正確的話):

data = {
    'time_frame1': [(x1,y1,z1),(x2,y2,z2)...(x4000,y4000,x4000)],
    'time_frame2': [(x1,y1,z1),(x2,y2,z2)...(x4000,y4000,x4000)],
    'time_frame3': [(x1,y1,z1),(x2,y2,z2)...(x4000,y4000,x4000)],
    'time_frame4': [(x1,y1,z1),(x2,y2,z2)...(x4000,y4000,x4000)],
    'time_frame5': [(x1,y1,z1),(x2,y2,z2)...(x4000,y4000,x4000)]
}

您可以輕松訪問數據:

data['time_frame1'][3999]
>>> (x4000,y4000,x4000)

如果您想更改/操縱時間范圍(在我們的例子中是關鍵):

# changed/manipulate time_frame1 
time_frame = 'new_time_frame'
data[time_frame] = data.pop('time_frame1')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM