如何使用列表的元素之一来调出特定的数据列表

问题描述

我有 4000 个原子,我有 5 个不同的时间框架:对于每个时间框架,每个原子都有 4000 组 XY 和 Z 坐标。我正在用python编写代码来从输出文件中读取数据。我得到了要在列表中调用的坐标我如何操作时间范围,以便当我调用特定时间范围内的特定原子而不是其他时间范围内的数据时。 谢谢你的帮助。 可以,当然: 这是一个示例: t = 0 原子数 = 4000 0.0 16.795961913825074 0.0 16.795961913825074 0.0 16.795961913825074

项目:ATOMS id type x y z vx vy vz fx fy fz

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

关注 x,y,z。其他 4 个时间范围也有类似的数据。目标是根据 id、坐标(x、y 和 z 单独)调用原子,并能够在给定的时间范围内选择原子。所以总而言之: x[id][x 或 y 或 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,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,'time_frame3': [(x1,'time_frame4': [(x1,'time_frame5': [(x1,x4000)]
}

您可以轻松访问数据:

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

如果你想改变/操纵时间范围(在我们的例子中是关键):

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