-2

I would like to order a dictionary by value from highest to lowest. THe dict is like this:

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

I got some code that works in one VM, but when i try in other one doesn't works.


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


In one VM i got this error, while in other one it works (using a virtualenv to execute the same script with same python version etc.)

TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

ANy ideas?

LinPy
  • 16,987
  • 4
  • 43
  • 57
  • 4
    Possible duplicate of [How do I sort a dictionary by value?](https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value) – Amit Gupta May 10 '19 at 07:29
  • @AmitGupta No, my problem is that my code works just in one VM, and another one not. –  May 10 '19 at 07:31
  • 2
    With respect to `dict = sorted(dicta.items(), key=lambda x: [int(x) for x in x[1].values()], reverse = True)`, a different variable name would be better, since `dict` is also a function name. See e.g. `dict([["A",1], ["B", 2]])` – sg.sysel May 10 '19 at 07:32
  • @sg.sysel This is ot the real name, is an example. –  May 10 '19 at 07:33
  • 1
    @hmar you should then ask the right question. The problem is not of sorting the dict but why your VM is giving error. The more specific you ask the question, the better chance of getting the answer. And use the right tags. – Amit Gupta May 10 '19 at 07:38
  • What is the Python version in both VMs? – Serge Ballesta May 10 '19 at 07:52
  • THe version are 3.5.1. –  May 10 '19 at 08:01

1 Answers1

1

There should be no error if you run the code. But you don't have a name for you dictionary.

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

dictSorted = sorted(dict_.items(), key=lambda x: [int(x) for x in x[1].values()], reverse = True)
Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
Moschte
  • 102
  • 1
  • 11
  • I just put the output of the dictionary, not the code. With this code it works in one VM and not works in other one using the same virtualenv. –  May 10 '19 at 07:34
  • 1
    Without the actual code, it is impossible to find the error. The example code should work everywhere. The only case I could foresee such an error would be if `x[1].values()` returns `None`, i.e. `int(x)` corresponds to `int(None)`. – sg.sysel May 10 '19 at 07:38
  • @sg.sysel and if that were the error, how can i make an exception when the value is empty? –  May 10 '19 at 07:43
  • 1
    The easiest way is to create a new dict and add only entries with not None values. https://stackoverflow.com/a/33797147/4685974 – Moschte May 10 '19 at 08:03
  • I just found in the dict this key: 'README': { None: None }, So the error could be there.. –  May 10 '19 at 08:28
  • Yes, I think you should remove all None values before you try to sort the dictionary. It is probably a good way to program a function which prepares your dict to sort it or to execute some other functions on the dict – Moschte May 10 '19 at 08:37