0

I am having problem with the zip command. I am fairly new to python and use it to manipulate inputs and outputs from the computaitonal chemistry software Gaussian.

My problem is the following. I extract that atomic number and the coordinates of the corresponding atom as two separate lists that i then zip together. The thing is, the newly created dictionnary, while correctly associating the atomic number to the corresponding coordinates, appears to shuffle the order of those pairs.

This is my code:

 str1 = lines[best_i + count].split()
 str1 = list(filter(None, str1))
 li_atoms.append(str1[1])
 li_lines.append(str1[3] + " " + str1[4] + " " + str1[5] + "\n")

 dict_lines = dict(zip(li_lines, li_atoms))
 new_line = str1[1] + " " + str1[3] + " " + str1[4] + " " + str1[5] + "\n"
 print(li_atoms)
 print(li_lines)
 print(new_line)

The first 4 lines are there to show how I create the lists I then zip. This is what I am getting:

['6', '6', '6', '6', '6', '6', '1', '1', '1', '1', '1', '8', '1', '8', '1', '1', '8', '1', '1', '8', '1', '1']  

['-0.934843 0.899513 0.316846\n',   
'-2.190300 1.027357 -0.281071\n',   
'-2.985223 -0.106295 -0.461617\n',   
'-2.532226 -1.360236 -0.051572\n',   
'-1.273703 -1.473149 0.546850\n',   
'-0.466792 -0.350395 0.736201\n',   
'-2.544435 2.004569 -0.604798\n',     
'-3.961332 -0.002247 -0.927404\n',     
'-3.152766 -2.239891 -0.193462\n',   
'-0.912684 -2.443561 0.876875\n',   
'0.509490 -0.433361 1.204588\n',   
'-0.107548 1.983966 0.517635\n',   
'-0.544217 2.786118 0.189973\n',   
'1.956069 -1.097186 -1.539946\n',   
'0.995285 -1.167127 -1.420910\n',   
'2.137200 -0.128588 -1.433795\n',   
'2.906601 -0.987077 1.012487\n',   
'3.770505 -1.401214 1.155911\n',   
'2.616140 -1.289889 0.114737\n',   
'2.585476 1.353513 -0.478949\n',   
'1.755805 1.714987 -0.116377\n',   
'2.904746 0.753703 0.231017\n']    

{'-1.273703 -1.473149 0.546850\n': '6',   
'-2.190300 1.027357 -0.281071\n': '6',   
'-2.544435 2.004569 -0.604798\n': '1',   
'2.585476 1.353513 -0.478949\n': '8',   
'2.906601 -0.987077 1.012487\n': '8',   
'-3.961332 -0.002247 -0.927404\n': '1',   
'0.995285 -1.167127 -1.420910\n': '1',   
'3.770505 -1.401214 1.155911\n': '1',   
'-2.985223 -0.106295 -0.461617\n': '6',   
'-0.544217 2.786118 0.189973\n': '1',   
'2.616140 -1.289889 0.114737\n': '1',   
'-0.912684 -2.443561 0.876875\n': '1',   
'-2.532226 -1.360236 -0.051572\n': '6',   
'1.755805 1.714987 -0.116377\n': '1',   
'2.137200 -0.128588 -1.433795\n': '1',   
'1.956069 -1.097186 -1.539946\n': '8',   
'0.509490 -0.433361 1.204588\n': '1',   
'-3.152766 -2.239891 -0.193462\n': '1',   
'-0.934843 0.899513 0.316846\n': '6',   
'2.904746 0.753703 0.231017\n': '1',   
'-0.107548 1.983966 0.517635\n': '8',   
'-0.466792 -0.350395 0.736201\n': '6'}

And I would like to have that dictonnary in the same order as both lists above. Thanks foir looking into it.

Dronan
  • 5
  • 3

1 Answers1

1

Python dictionaries have no set order. The behaviour you see is entirely normal and correct.

Quoting the Python documentation for the dict.items() method:

Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions.

See Why is the order in dictionaries and sets arbitrary? for why that is.

If you require ordering to be preserved, use a collections.OrderedDict() instead:

from collections import OrderedDict

 dict_lines = OrderedDict(zip(li_lines, li_atoms))
Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343