From: tutor-bounces+ramit.prasad=jpmorgan....@python.org [mailto:tutor-bounces+ramit.prasad=jpmorgan....@python.org] On Behalf Of lina Sent: Tuesday, September 27, 2011 10:35 PM Cc: tutor Subject: Re: [Tutor] map one file and print it out following the sequence
Hi, Thanks for both of your reply. 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. Thanks. =================================================================== This is something I wrote *really* quick and is untested. Hopefully someone on this list can spot any error I made. mapping={} with open("cur.itp") as f: for line in f.readlines(): parts=line.strip().split() if len(parts)==8: mapping[parts[4]]=parts[0] with open("processedpdb") as f: proccessed = [ line.split() for line in f.readlines() ] processed.sort( key=lambda x: mapping.get( x[2], 0 ) ) # use 0 to put items without a mapping at the # top of the file because they are probably an error For line in array: print ' '.join( line ) Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor