0

I have a script that recollect data and insert in a dictionary. I use the same script in different vm's and just works in one of them.

The dict is like that:

{'data1': {'/home/data1': 273}, 'data2': {'/home/data2': 2}, 'data3': {'/home/data3': 10}, 'data4': {'/home/data4': 1}}

And i should order by the value of the numbers, so i should have the next dict:

{'data1': {'/home/data1': 273},'data3': {'/home/data3': 10}, 'data2': {'/home/data2': 2},'data4': {'/home/data4': 1}}

he code of the sorted dict is like that:

directorios = sorted(dicta.items(), key=lambda x: [int(x) for x in x[1].values()], reverse = True)

The curious thing is that using the same script and getting the same dictionary but with different values in different virtual machines, in one of them i get the script works and the dictionary its perfectly sorted but in the other one i get the next error:

    directorios = sorted(dicta.items(), key=lambda x: [int(x) for x in x[1].values()], reverse = True)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

Both VM's have the same Python Version. Any ideas?

  • You get that `TypeError` when you run this on this exact data? Because I don't see how that would happen. – glibdud May 09 '19 at 14:58
  • It seems that you have empty value in the dataset. Checking it out could help. – Kidsunbo May 09 '19 at 14:59
  • Try printing the version from the script. Add `import sys; print(sys.version)` and double-check the version is the same. Also check the data is the same. – gustavovelascoh May 09 '19 at 15:00
  • 1
    And regarding the sort, if you're converting your result (`directorios`) back into a dict, whether it will maintain the order depends on exactly what version of python is used. Check the version on all VMs. – glibdud May 09 '19 at 15:00
  • @glibdud The structure of the data are the same but with different values.The script is the same version becaus is downloaded from GIT. and python version are the same. I tried to execute in the same virtualenv –  May 09 '19 at 15:03
  • @hmar The script, sure, but the version of python installed on the VMs may be different. – glibdud May 09 '19 at 15:05
  • I tried to execute in the same virtualenv @glibdud –  May 09 '19 at 15:06
  • @hmar Actually, I just realized the tag is python-3.5. Python 3.5 doesn't maintain order in standard dicts. You'll need to use [`collections.OrderedDict()`](https://docs.python.org/3.5/library/collections.html#collections.OrderedDict) if you want your dict to maintain insertion order. – glibdud May 09 '19 at 15:09
  • @glibdud Im using 3.5.1 and with this code i order a dictionary correctly. I'm just checked again the output. If you dont believe, i can send you all my code in PM. –  May 09 '19 at 15:14
  • @hmar Any ordering of a dict before python 3.7 would have to be considered an implementation detail, and it's unwise to rely on it ([more info](https://stackoverflow.com/questions/39980323/are-dictionaries-ordered-in-python-3-6/39980744)). If you want help, you'll need to provide enough code in your question body to reproduce the issue you're having. See [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – glibdud May 09 '19 at 15:18
  • @hmar, how are you checking what version of Python are you running? (Not what version is installed) – gustavovelascoh May 09 '19 at 15:32
  • @gustavovelascoh I running the script in a virtualenv with same Python version.. –  May 10 '19 at 07:18

0 Answers0