How to count top 10 most common values in a dict in python

前端 未结 1 423
既然无缘
既然无缘 2020-12-10 03:04

I\'m new to python and programming in general and so please be kind. I\'m trying to analyze a csv file with music information and return the top n most listened to bands. Fr

相关标签:
1条回答
  • 2020-12-10 04:05

    Initialize the Counter once, let the keys be artists, and augment a key (artist) each time through the loop:

    c = Counter()
    for d in entries:
        arts = d['artist']
        c[arts] += 1
    print(c.most_common(10))
    

    When arts is a string, then c = Counter(arts) counts the characters in arts:

    In [522]: collections.Counter('Led Zepplin')
    Out[522]: Counter({'e': 2, 'p': 2, ' ': 1, 'd': 1, 'i': 1, 'L': 1, 'l': 1, 'n': 1, 'Z': 1})
    

    In contrast:

    In [523]: c = collections.Counter()
    
    In [524]: c['Led Zepplin'] += 1
    
    In [525]: c['The Rolling Stones'] += 1
    
    In [526]: c.most_common()
    Out[526]: [('Led Zepplin', 1), ('The Rolling Stones', 1)]
    

    Alternatively, as Jon Clements points out, build a list of all the artists, and then count the list:

    c = Counter(d['artist'] for d in entries)
    print(c.most_common(10))
    

    Note that the above uses a generator expression to avoid building a (possibly) large temporary list, and at the same time has a much more succinct, readable syntax.

    0 讨论(0)
提交回复
热议问题