I've used the following approach many times to generate a list of tuples from the contents of a dictionary:
dispositions = list(dispositions.items())
In this case, the keys are different types of ways a patient can leave the emergency department, and the values are counts of those types. Now, I wanted to sort this data based on the second item in each of the tuples, so I tried this:
dispositions = list(dispositions.items()).sort(key=lambda x: x[1])
To my surprise, when I ran the code, I found that dispositions had been set to None. I tried breaking it into two parts, as follows:
dispositions = list(dispositions.items())
dispositions.sort(key=lambda x: x[1])
This works! I got my list of sorted tuples. So, I've already solved my problem, but I want to know why the first option didn't work so that I can be a better person (better programmers are better people). Can anyone help me out here?
question from:
https://stackoverflow.com/questions/65909352/converting-list-into-dictionary-of-sorted-tuples 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…