Search code examples
pythonwindowspython-3.xlambdapsutil

Python lambda function to sort list according to dictionary


The sample codes bellow retrieve all the runing procces and print them. They've bee written following the 3rd example here and final one from here. Problem is I cannot figure out why only the first one retrieves process sorted as desired.

I think it is related to the lambda function construction. But the rightly running sample, the first one, seems to mix the local p variable of the for statement with the p.dict dictionary, making me get stuck.

First Sample:

import psutil

procs = []

for p in psutil.process_iter():
    try:
        p.dict = p.as_dict(['pid', 'name'])
    except psutil.NoSuchProcess:
        pass
    else:
        procs.append(p)

processes = sorted(procs, key=lambda p: p.dict['name'], reverse=False)

print(processes)

The second sample:

import psutil

procs = []

for proc in psutil.process_iter():
    try:
        procs_dic = proc.as_dict(['pid', 'name'])
    except psutil.NoSuchProcess:
         pass
    else:
        procs.append(proc)

processes = sorted(procs, key=lambda ordem: procs_dic['name'], reverse=False)

print(processes)

Solution

  • Your second code snippet's lambda looks up 'name' in the same dictionary no matter what object it's passed; how could that possibly work?

    Your third doesn't even seem to be trying to sort the processes; I'm not sure what it has to do with the question.

    The change you've made to turn the first snippet into the second is evidently motivated by your concern that the first

    seems to mix the local p variable of the for statement with the p.dict dictionary

    and I'd be happy to help but I'm afraid I don't understand what problem it is you see. Perhaps the following may help? There are two variables here called p. The first is used in the loop over processes; each time round the loop its value is a process object, and we give that process object a dict attribute containing an entry for 'name'. The second is the argument to your anonymous function (lambda): its value is also always a process object. You could give them different names if you wanted and it wouldn't break anything, but actually I think it's clearer as it is: in this little bit of code, p is what you call a variable whose value is a process object. But nothing's getting "mixed up".