How to count the frequency of the elements in an unordered list?

后端 未结 30 2905
时光说笑
时光说笑 2020-11-22 02:37

I need to find the frequency of elements in an unordered list

a = [1,1,1,1,2,2,2,2,3,3,4,5,5]

output->

b =         


        
相关标签:
30条回答
  • 2020-11-22 02:45
    from collections import OrderedDict
    a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
    def get_count(lists):
        dictionary = OrderedDict()
        for val in lists:
            dictionary.setdefault(val,[]).append(1)
        return [sum(val) for val in dictionary.values()]
    print(get_count(a))
    >>>[4, 4, 2, 1, 2]
    

    To remove duplicates and Maintain order:

    list(dict.fromkeys(get_count(a)))
    >>>[4, 2, 1]
    
    0 讨论(0)
  • 2020-11-22 02:45

    Yet another solution with another algorithm without using collections:

    def countFreq(A):
       n=len(A)
       count=[0]*n                     # Create a new list initialized with '0'
       for i in range(n):
          count[A[i]]+= 1              # increase occurrence for value A[i]
       return [x for x in count if x]  # return non-zero count
    
    0 讨论(0)
  • 2020-11-22 02:45

    For the record, a functional answer:

    >>> L = [1,1,1,1,2,2,2,2,3,3,4,5,5]
    >>> import functools
    >>> >>> functools.reduce(lambda acc, e: [v+(i==e) for i, v in enumerate(acc,1)] if e<=len(acc) else acc+[0 for _ in range(e-len(acc)-1)]+[1], L, [])
    [4, 4, 2, 1, 2]
    

    It's cleaner if you count zeroes too:

    >>> functools.reduce(lambda acc, e: [v+(i==e) for i, v in enumerate(acc)] if e<len(acc) else acc+[0 for _ in range(e-len(acc))]+[1], L, [])
    [0, 4, 4, 2, 1, 2]
    

    An explanation:

    • we start with an empty acc list;
    • if the next element e of L is lower than the size of acc, we just update this element: v+(i==e) means v+1 if the index i of acc is the current element e, otherwise the previous value v;
    • if the next element e of L is greater or equals to the size of acc, we have to expand acc to host the new 1.

    The elements do not have to be sorted (itertools.groupby). You'll get weird results if you have negative numbers.

    0 讨论(0)
  • 2020-11-22 02:48

    I am quite late, but this will also work, and will help others:

    a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
    freq_list = []
    a_l = list(set(a))
    
    for x in a_l:
        freq_list.append(a.count(x))
    
    
    print 'Freq',freq_list
    print 'number',a_l
    

    will produce this..

    Freq  [4, 4, 2, 1, 2]
    number[1, 2, 3, 4, 5]
    
    0 讨论(0)
  • 2020-11-22 02:48
    a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
    
    # 1. Get counts and store in another list
    output = []
    for i in set(a):
        output.append(a.count(i))
    print(output)
    
    # 2. Remove duplicates using set constructor
    a = list(set(a))
    print(a)
    
    1. Set collection does not allow duplicates, passing a list to the set() constructor will give an iterable of totally unique objects. count() function returns an integer count when an object that is in a list is passed. With that the unique objects are counted and each count value is stored by appending to an empty list output
    2. list() constructor is used to convert the set(a) into list and referred by the same variable a

    Output

    D:\MLrec\venv\Scripts\python.exe D:/MLrec/listgroup.py
    [4, 4, 2, 1, 2]
    [1, 2, 3, 4, 5]
    
    0 讨论(0)
  • 2020-11-22 02:49

    This approach can be tried if you don't want to use any library and keep it simple and short!

    a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
    marked = []
    b = [(a.count(i), marked.append(i))[0] for i in a if i not in marked]
    print(b)
    

    o/p

    [4, 4, 2, 1, 2]
    
    0 讨论(0)
提交回复
热议问题