I have a dictionary that looks like this:
{'key_info': (rank, raw_data1, raw_data2),
'key_info2': ...}
Basically I need back a list of the keys in sorted order, that is sorted based on the rank field in the tuple.
My code looks something like this right now (diffs is the name of the dict above):
def _sortRanked(self):
print(type(self.diffs))
return sorted(self.diffs.keys(), key=lambda x: x[1], reverse=True)
that right now returns this when I run it:
return sorted(self.diffs.keys(), key=lambda x: x[1], reverse=True)
IndexError: string index out of range
keys()only gives you keys, not values, so you have to use the keys to retrieve values from the dict if you want to sort on them:Since you’re sorting on
rank, which is the first item in the tuple, you don’t need to specify which item in the value tuple you want to sort on. But if you wanted to sort onraw_data1: