user9814201
user9814201

Reputation:

Print top "n" entries in a dictionary

Here's my current code

pN ={'dave': 10, 'jacinta': 10, 'james': 8, 'john': 6, 'jack': 3, 'sam': 2}
highestCount = max(pN.values())

for k, v in pN.items():
  if v == highestCount:
    print(v,k)

However this only prints the top user and if that position is shared, prints it again as such

10 dave
10 jacinta

I need to be able to print any amount of top users (n) and have it formatted as such, e.g. for n = 5:

10 john, jacinta, 
8 james
6 john
3 jack
2 sam

Upvotes: 14

Views: 10115

Answers (5)

Eric Duminil
Eric Duminil

Reputation: 54263

Since you want to group by scores, it would make sense to use itertools.groupby:

scores ={'dave': 10, 'jacinta': 10, 'james': 8, 'john': 6, 'jack': 3, 'sam': 2}

from itertools import groupby
from operator import itemgetter

get_score = itemgetter(1)

def group_users_by_score(scores, n=-1):
    sorted_users = sorted(scores.items(), key=get_score, reverse=True)
    top_n = sorted_users[:n]
    return groupby(top_n, key=get_score)

def display_top_users(scores, n=-1):
    for score, users in group_users_by_score(scores, n):
        print("%3d %s" % (score, ', '.join(u for (u,s) in users)))

As an example:

>>> display_top_users(scores, 3)
 10 dave, jacinta
  8 james
>>> display_top_users(scores)
 10 dave, jacinta
  8 james
  6 john
  3 jack

Upvotes: 0

Bijoy
Bijoy

Reputation: 1131

You should try it with collections.defaultdict with in-built sorted() funct.

from collections import defaultdict

def sort_it(_dict, n):
    result = defaultdict(list)
    for name, num in _dict.items():
        result[num].append(name)
    return sorted(result.items(), reverse=True)[:n]

>>> pN = {'dave': 10, 'jacinta': 10, 'james': 8, 'john': 6, 'jack': 3, 'sam': 2}
>>> top3 = sort_it(pN, 3)
[(10, ['jacinta', 'dave']), (8, ['james']), (6, ['john'])] # Output

Upvotes: 0

user3483203
user3483203

Reputation: 51165

Use a collections.defaultdict, swap your keys and values

from collections import defaultdict
dct = defaultdict(list)

for k, v in pN.items():
  dct[v].append(k)

# defaultdict(<class 'list'>, {10: ['dave', 'jacinta'], 8: ['james'], 6: ['john'], 3: ['jack'], 2: ['sam']})

Use sorted for output:

for k, v in sorted(dct.items(), reverse=True):
  print(k, ', '.join(v))

# Result

10 dave, jacinta
8 james
6 john
3 jack
2 sam

function to return top n users (treats ties as one entry):

def top_n(d, n):
  dct = defaultdict(list) 
  for k, v in d.items():
    dct[v].append(k)      
  return sorted(dct.items())[-n:][::-1]

top_n(pN, 3)

# [(10, ['dave', 'jacinta']), (8, ['james']), (6, ['john'])]

Using a defaultdict is simple and fast, and here are some timings to prove it:

Functions that will be timed

def chris_z(d, n):
  dct = defaultdict(list) 
  for k, v in d.items():
    dct[v].append(k)      
  return sorted(dct.items())[-n:][::-1]

def tim_lombard(score_dict, n):
  lot = [(k,v) for k, v in score_dict.items()] #make list of tuple from scores dict
  nl = []
  while len(lot)> 0:
      nl.append(max(lot, key=lambda x: x[1]))
      lot.remove(nl[-1])

def ajax(d, n:'n_users', top = True):
  _ranks = sorted(d.values())
  _ranks = _ranks[-n:] if top else _ranks[:n]
  return {i:[a for a, b in d.items() if b == i] for i in _ranks}

Results

x = [''.join(i) for i in itertools.permutations('chrisz', 6)]    
y = [random.randint(0, 100) for _ in range(720)]  
z = dict(zip(x, y))

In [40]: %timeit chris_z(z, 500)
110 µs ± 259 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [42]: %timeit tim_lombard(z, 500)
26.2 ms ± 60 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [43]: %timeit ajax(z, 500)
15.3 ms ± 227 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Upvotes: 11

Timothy Lombard
Timothy Lombard

Reputation: 967

Would this work for you?

pN ={'dave': 10, 'jacinta': 10, 'james': 8, 'john': 6, 'jack': 3, 'sam': 2}

def top_n_scores(n, score_dict):
  ''' returns the n scores from a name:score dict'''
  lot = [(k,v) for k, v in pN.items()] #make list of tuple from scores dict
  nl = []
  while len(lot)> 0:
      nl.append(max(lot, key=lambda x: x[1]))
      lot.remove(nl[-1])

  return nl[0:n]   

To get the top 4 scores:

top_n_scores(4, pN) 

[('dave', 10), ('jacinta', 10), ('james', 8), ('john', 6)]

Upvotes: 1

Ajax1234
Ajax1234

Reputation: 71461

You can use sorted and a dictionary comprehension:

from typing import Dict, List
def ranking(d, n:'n_users', top = True) -> Dict[int, List[str]]:
  _ranks = sorted(d.values())
  _ranks = _ranks[-n:] if top else _ranks[:n]
  return {i:[a for a, b in d.items() if b == i] for i in _ranks}

pN ={'dave': 10, 'jacinta': 10, 'james': 8, 'john': 6, 'jack': 3, 'sam': 2}
for a, b in sorted(ranking(pN, 10).items(), key=lambda x:x[0], reverse=True):
  print('{} {}'.format(a, ', '.join(b)))

Output:

10 dave, jacinta
8 james
6 john
3 jack
2 sam

Edit: for any number of top users, pass the value to the function:

_r = ranking(pN, 5) #for the top 5 users

Upvotes: 1

Related Questions