I am trying to rank a list of dictionaries based on a common criteria. So my list looks something like this:
d = [{'key1': 0, 'key2': 1}, {'key1': 1, 'key2': 0}]
I want to obtain the rank (in reverse order) of each dictionary according to a criteria that I may specify. So, if the criteria is 'key1', the output should be:
1 0
If the criteria is 'key2', then the output should be:
0 1
I tried the following function:
def rank_simple(list, criteria):
return sorted(range(len(list)), reverse = True, key = type(list).__getitem__(list)[criteria])
which gives the error:
TypeError: __getitem__() takes exactly one argument (0 given)
and
def rank_simple(list, criteria):
return sorted(range(len(list)), reverse = True, key = list.__getitem__[criteria])
which gives the error:
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
as per suggestions in Efficient method to calculate the rank vector of a list in Python
How should I solve this problem. Please note that I do not want to use scipy or numpy for this (if possible)