[Tutor] map one file and print it out following the sequence

Alan Gauld alan.gauld at btinternet.com
Wed Sep 28 10:15:40 CEST 2011


On 28/09/11 04:34, lina wrote:

> File 1 is:
>
>       3         C     1  CUR     C19     1    0.200  12.0110
>       4       CR1     1  CUR     C20     1   -0.060  12.0110
>       5        HC     1  CUR     H20     1    0.060   1.0080
>
> File 2 is:
> ATOM      2  H20 CUR     1      30.338  28.778  -6.812  1.00  0.00
> ATOM      4  C20 CUR     1      31.394  28.922  -7.039  1.00  0.00
> ATOM      5  C19 CUR     1      31.790  29.357  -8.323  1.00  0.00
>
> I wish to get:
>
> ATOM      5  C19 CUR     1      31.790  29.357  -8.323  1.00  0.00
> ATOM      4  C20 CUR     1      31.394  28.922  -7.039  1.00  0.00
> ATOM      2  H20 CUR     1      30.338  28.778  -6.812  1.00  0.00
>
> The dictionary is C19 C20 H20 sequence read from file 1 field 5.
> rearrange the file 2 field 3 following the sequence of C19, C20, H20.

OK, so just to confirm: you want to sort file 2 based on the order of 
field 5 in file 1.

So read file2 into a dictionary keyed by field 3
Then read file 1 and output the dictionary entry associated with field 5

So in pseudo code:

d = dict()
for line in file1:
     d[ line.split()[2] ] = line

for line in file1:
     print d[ line.split()[4] ]

Is that what you want?

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list