2017-01-18 68 views
0

我已经过了这个问题,经过几个小时的尝试,我想问一个有更多python经验的人,而不是我(这应该没有问题,因为我是一个Python初学者)。提取数据类型为float64的.xyz文件的3x3矩阵,或者将三个1x3矩阵合并为一个3x3矩阵

有8分我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) 

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

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

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

回答

0

这里有一种方法可以做到这

# 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 
+0

非常感谢彼得,第一个已经为我做,我不得不加上“matrix_fin =垫(xyz_matrix)”和我的基质是如我所愿。 – Anna