在Python中搜索字典

时间:2011-12-11 00:02:22

标签: python

我想搜索字典"C"中的一个组件。

对于那个组件,我有大约6对(x,y,z)。我想搜索直到没有更多并将整数分配给变量:x_value,然后是y,然后是z

Guanine = {"C": [(6.958, -5.037, 2.040), (7.355, -4.850, 3.500), 
                 (6.601, -5.985, 4.170), (6.713, -7.099, 3.130),
                 (5.627, -8.157, 3.190), (4.259, -1.254, 0.410),
                 (4.530, -2.553, 0.900), (4.456, -4.566, 1.600),
                 (5.830, -2.919, 1.170), (6.712, -0.955, 0.570)]}

例如:

Atom type: X-value: Y-Value: Z-Value:

C          6.958    -5.037    2.040
C2         6.601    -5.985    4.170

etc...

我想这样做,直到没有更多价值。

1 个答案:

答案 0 :(得分:3)

print "Type:\tindex:\tX:\tY:\tZ:"
for atom_type, coordinates in Guanine.items():
    for i, (x,y,z) in enumerate(coordinates):
        print "%s\t%d\t%5.3f\t%5.3f\t%5.3f" % (atom_type, i, x, y, z)

输出:

Type:   index:  X:      Y:      Z:
C       0       6.958   -5.037  2.040
C       1       7.355   -4.850  3.500
C       2       6.601   -5.985  4.170
C       3       6.713   -7.099  3.130
C       4       5.627   -8.157  3.190
C       5       4.259   -1.254  0.410
C       6       4.530   -2.553  0.900
C       7       4.456   -4.566  1.600
C       8       5.830   -2.919  1.170
C       9       6.712   -0.955  0.570
相关问题