upatients = {
'Junipero':[ 37, 114, 'M', 'I', 6, 2],
'Sunita':[22, 27, 'F', 'B', 9, 5],
'Issur':[ 38, 48, 'D', 'W', 4, 1],
'Luitgard':[ 20, 105, 'M', 'L', 1, 4],
'Rudy':[ 20, 27, 'D', 'O', 9, 5],
'Ioudith':[ 19, 93, 'D', 'I', 4, 3]
}
for key in upatients:
patients = key
print(patients)
I am trying to access the values in the [5]
position in the list and sort the list based on those values. I am having trouble printing those values.
CodePudding user response:
Use the key
option to sorted()
to use an element of the value when sorting.
def sort_dict_keys(d, index):
return sorted(d, key = lambda p: d[p][index])
print(sort_dict_keys(upatients, 5))
CodePudding user response:
To print the item at the 5th index in each list you can use:
for key in upatients:
patients = upatients[key]
print(patients[5])
Here is one way of getting a sorted list of patients:
tmp = [(upatients[key][5], key) for key in upatients]
sort(tmp)
new_list = [x[1] for x in tmp]
print(new_list)
Output:
['Issur', 'Junipero', 'Ioudith', 'Luitgard', 'Rudy', 'Sunita']