Rank of a variable in a counter

Viewed 1840

I have data stored in a counter e.g.

industries = Counter({'Automotive': 17, 'Commercial Banks': 10, 'Insurance': 4, 'Hospitals': 2, 'Other': 2})

I am interested in looking up the 'index' (i.e. 'rank') of a particular variable. For example, if I am interested in Automotive to return that it has the highest key, commercial banks has the second highest.

At the moment I am converting to a list, and then looking up the index of that item. e.g.

industries_list = [key[0] for key in industries.most_common()]
rank_of_automotive = industries_list.index("Automotive")

Is there a way of doing this directly form the counter without the need to convert to a list?

[Obviously, I am aware that my current code does not deal with variables of the same value correctly - i.e. although Hospitals and Other which both have value 2, my could would return a different rank for them. This isn't a big issue for me, but if it were the case that both returned the same value that would also be find]

2 Answers
Related