从列表中填充数组

时间:2013-10-11 11:46:15

标签: python numpy

我是python的新手,我在尝试使用列表数据填充数组时遇到问题

例如我的列表如下:

Item TIMESTEP
0
STEPS
Id mol Sp x y z
1 1 0.25 0.63 0.58
2 1 0.85 0.96 0.10
3 2 0.36 0.45 0.89
4 3 0.45 0.16 0.22
5 3 0.85 0.65 0.77

Item TIMESTEP
10
STEPS
Id mol Sp x y z
1 1 0.85 0.33 0.68
2 1 0.65 0.26 0.20
3 2 0.56 0.35 0.79
4 3 0.75 0.36 0.12
5 3 0.65 0.75 0.87

......并继续像1000那样 我想要做的是让一个数组只包含彼此相邻的X,Z,Y值,但只是第一个和下一个,因为我只需要使用坐标来获得相对值,所以将每个时间步长与前一个时间步长进行比较。 直到现在我都有这个,但阵列没有给出任何结果......你能告诉我什么是错的或如何纠正它?

numofmol = 600
B = np.zeros((numofmol,6)) #array filled with ceros with 6 columns and 2688 lines
A = list() # empty list A

c=0


with open('./square/output/Sq.lammpstrj') as inf:
    for line in inf:
        parts = line.split() # split line into parts 
        #print parts
        if len(parts) > 1 :
            if parts[1] == 'TIMESTEP' :
                c +=1
            #print c
        if len(parts) == 5 and numofmol >= parts[0] > 0  :
            if c % 2 == 0:
                    B[parts[0],0] = parts[2]
                    B[parts[0],1] = parts[3]
                    B[parts[0],2] = parts[4]
            else:
                    B[parts[0],3] = parts[2]
                    B[parts[0],4] = parts[3]
                    B[parts[0],5] = parts[4]

            print B

提前谢谢

1 个答案:

答案 0 :(得分:0)

有点难以获得,你真正想要什么,但你可以尝试这样的事情:

data, c = {}, 0
with open('./square/output/Sq.lammpstrj') as inf:
    for line in inf:
        parts = line.split()
        if len(parts) > 1 and parts[1] == 'TIMESTEP':
            c += 1
        if len(parts) == 5:
            if c % 2 == 1:
                data[parts[0]] = parts[2:]
            else:
                data[parts[0]] += parts[2:]

B = np.array(data.values())
相关问题