提取数据类型为 float64 的 .xyz 文件的 3x3 矩阵或将三个 1x3 矩阵合并为一个 3x3 矩阵

extracting 3x3 matrix of a .xyz file with datatype float64 or merging three 1x3 matrices to one 3x3 matrix

我已经解决了这个问题,经过几个小时的尝试,我想请教在 python 方面比我更有经验的人(这应该没问题,因为我是 python初学者)。

我的 input.xyz 文件有八个点,看起来像这样:

15.486586, 46.798954, -6.232800, 15.445764, 46.807576, -6.249205, -0.040822,0.008622, -0.016405, 0.044832;
6.233575, 48.083754, -4.223557, 6.187027, 48.090785, -4.243389, -0.046548, 0.007031, -0.019832, 0.051083;
-2.159452, 40.818712, -3.104244, -2.200572, 40.818489, -3.120266, -0.041120,-0.000223, -0.016022, 0.044132;
45.554111, 131.689322, 1.525740, 45.452954, 131.721406, 1.483290, -0.101157,0.032084, -0.042450, 0.114298;
28.315109, 146.107918, 2.897549, 28.235633, 146.131800, 2.864060, -0.079476,   0.023882, -0.033489, 0.089489;
7.303209, 138.223347, 4.702106, 7.250850, 138.242379, 4.679564, -0.052359, 0.019032, -0.022542, 0.060098;
-32.211983, 148.909744, 12.919538, -32.279077, 148.907541, 12.876267,-0.067095, -0.002203, -0.043271, 0.079868;
-48.926024, 180.295215, 20.142896, -49.008547, 180.275117, 20.127614,-0.082523, -0.020098, -0.015282, 0.086299;

“;”分隔每个点,一个点的前三个值是 x、y 和 z 值。所以我想用它们的 xyz 值取三个点并将它们写在 python 的矩阵中。 这是我到目前为止得到的:

# creating empty list for append
xyz_matrx = []


counter = 0
for line in xyz:
    counter += 1
# counter to get only first three columns
    if counter%4 == 0:
        break

    # deleting whitespaces and selecting wanted data
    linevalues = line.strip().split(",")
    x = (linevalues[0:1])
    y = (linevalues[1:2])
    z = (linevalues[2:3])
    xyz_matrx.append(x)

#flatten because xyz_matrix is a list within list
# values converting into float64, because string won't work for following    
#work
flattenedx = [val for sublist in xyz_matrx for val in sublist]
matr_flatx = [float(i) for i in flattenedx]
A_matrx = mat(matr_flatx)

有了这个,我得到一个 1x3 矩阵,其中 xyz 点在矩阵中是水平的,但我想要矩阵中的三列,代表每个点和代表 xyz 值的行,一个 3x3 矩阵数据类型 float64。 如果我用索引改变某些东西,我只会得到 string88 矩阵。 我可以为其他两个点再创建两个列表,然后我有三个 1x3 矩阵,但是“.append”不起作用,因为我没有二维矩阵?

我知道我的代码效率不高,但我希望有人能理解我的问题并能帮助我。

简而言之:我有一个输入 .xyz 文件,只有每个点的前三个值(x、y、z 坐标)是相关的,我想要 xyz 的三个点及其三个坐标中的每一个一个 3x3 矩阵(第一列垂直:第一个点,xyz,第二列垂直:第二个点与 xyz 和第三列第三个点与 xyz 垂直向下),数据类型必须是 float64.

这是您可以做到的一种方法

# creating empty list for append
xyz_matrix = []
counter = 0

with open('input.xyz', 'r') as f:
    for line in f:
        # Add the first three values to the matrix
        xyz_matrix.append(map(float, line.strip().split(",")[0:3]))
        counter += 1
        if counter == 3:
            break

# Transpose
xyz_matrix = zip(*xyz_matrix)

print xyz_matrix

你最终得到了一个元组列表,但那应该没问题。

这更直接,但不太笼统

# Creating an empty 2D list
xyz_matrix = [[], [], []]
counter = 0

with open('input.xyz', 'r') as f:
    for line in f:
        # Add the first three values to the matrix
        values = map(float, line.strip().split(",")[0:3])

        # Append x, y, z to each rows 0, 1, 2 respectively
        for i in range(3):
            xyz_matrix[i].append(values[i])

        counter += 1
        if counter == 3:
            break

print xyz_matrix