How to get unique values with respective occurrence count from a list in Python?

前端 未结 11 2051
孤独总比滥情好
孤独总比滥情好 2020-12-13 13:13

I have a list which has repeating items and I want a list of the unique items with their frequency.

For example, I have [\'a\', \'a\', \'b\', \'b\', \'b\']

相关标签:
11条回答
  • 2020-12-13 13:38

    If you are willing to use a 3rd party library, NumPy offers a convenient solution. This is particularly efficient if your list contains only numeric data.

    import numpy as np
    
    L = ['a', 'a', 'b', 'b', 'b']
    
    res = list(zip(*np.unique(L, return_counts=True)))
    
    # [('a', 2), ('b', 3)]
    

    To understand the syntax, note np.unique here returns a tuple of unique values and counts:

    uniq, counts = np.unique(L, return_counts=True)
    
    print(uniq)    # ['a' 'b']
    print(counts)  # [2 3]
    

    See also: What are the advantages of NumPy over regular Python lists?

    0 讨论(0)
  • 2020-12-13 13:39

    A solution without hashing:

    def lcount(lst):
       return reduce(lambda a, b: a[0:-1] + [(a[-1][0], a[-1][1]+1)] if a and b == a[-1][0] else a + [(b, 1)], lst, [])
    
    >>> lcount([])
    []
    >>> lcount(['a'])
    [('a', 1)]
    >>> lcount(['a', 'a', 'a', 'b', 'b'])
    [('a', 3), ('b', 2)]
    
    0 讨论(0)
  • 2020-12-13 13:48

    the "old school way".

    >>> alist=['a', 'a', 'b', 'b', 'b']
    >>> d={}
    >>> for i in alist:
    ...    if not d.has_key(i): d[i]=1  #also: if not i in d
    ...    else: d[i]+=1
    ...
    >>> d
    {'a': 2, 'b': 3}
    
    0 讨论(0)
  • 2020-12-13 13:50

    Here's one way:

    your_list = ['a', 'a', 'b', 'b', 'b']
    
    count_dictionary = {}
    
    for letter in your_list:
    
        if letter in count_dictionary:
    
            count_dictionary[letter] +=1 
    
        else:
    
            count_dictionary[letter] = 1
    
    0 讨论(0)
  • 2020-12-13 13:51

    If your items are grouped (i.e. similar items come together in a bunch), the most efficient method to use is itertools.groupby:

    >>> [(g[0], len(list(g[1]))) for g in itertools.groupby(['a', 'a', 'b', 'b', 'b'])]
    [('a', 2), ('b', 3)]
    
    0 讨论(0)
  • 2020-12-13 13:55
    >>> mylist=['a', 'a', 'b', 'b', 'b']
    >>> [ (i,mylist.count(i)) for i in set(mylist) ]
    [('a', 2), ('b', 3)]
    
    0 讨论(0)
提交回复
热议问题