首页 > 解决方案 > 如何使用列表的元素之一来调出特定的数据列表

问题描述

我有 4000 个原子,我有 5 个不同的时间框架:对于每个时间框架,每个原子都有 4000 组 XY 和 Z 坐标。我正在用 python 编写代码来从输出文件中读取数据。我得到了要在列表中调用的坐标,我如何操作时间范围,以便在调用特定时间范围内的特定原子时调用,而不是调用其他时间范围内的数据。谢谢你的帮助。确定:这是一个示例: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 分别)调用原子,并能够在给定的时间范围内选择原子。总而言之: 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()

标签: pythondata-sciencecomputer-sciencedata-entry

解决方案


编辑-既然你已经给出了一个例子,我建议你像这样存储数据:

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')

推荐阅读