On 2011-10-12 10:27, lina wrote:
$ python3 map-to-itp.py
{'O4': '2', 'C19': '3', 'C21': '1'}
{'C': '3'}
{'C': '2'}
{'C': '1'}
for print(mapping) part, {'O4': '2', 'C19': '3', 'C21': '1'} the value
doesn't keep the 1, 2, 3 order any more.
That's fine, because "mapping" is a dictionary which has no order. From
the tutorial
(http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries):
"It is best to think of a dictionary as an unordered set of key: value
pairs, with the requirement that the keys are unique (within one
dictionary)."
What you want (as far as I understand it) is sorting the lines in
"pdbone.pdb" based on the positions in file "itpone.itp". The connection
between both files is the column with the values "O4", "C19", "C21", ...
(= your keys). You've already succesfully built a dictionary in which
you saved the position for every key.
For the sorting you could now build a list of tuples of all lines in
"pdbone.pdb" you want to sort where the first element in the tuple is
the position and the second the line itself. Then you can easily sort
this temporary list and write the new ordered lines back to the file:
def sortoneblock(cID):
text = fetchonefiledata(INFILENAME)
temp = [] # create an empty temporary list
for line in text:
blocks = line.strip().split()
if len(blocks) == 11 and blocks[3] == "CUR" and blocks[4] ==
cID and blocks[2] in mapping.keys():
temp.append((mapping[blocks[2]], line)) # add a tuple to
the list which has the following format: (position from the dictionary,
complete line)
# the following line just shows you, what we have done so far. You
can delete it without consequences.
for line in temp: print(line)
temp.sort() # this sorts the list based on the position
# the following line prints the sorted list (just the original line
without the position elements). If you want to write the result back to
the file you have to exchange "print()"
for line in temp: print(line[1])
Bye, Andreas
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor