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

后端 未结 30 2876
时光说笑
时光说笑 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 03:06

    To find unique elements in the list:

    a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
    a = list(set(a))
    

    To find the count of unique elements in a sorted array using dictionary:

    def CountFrequency(my_list): 
    # Creating an empty dictionary  
    freq = {} 
    for item in my_list: 
        if (item in freq): 
            freq[item] += 1
        else: 
            freq[item] = 1
    
    for key, value in freq.items(): 
        print ("% d : % d"%(key, value))
    
    # Driver function 
    if __name__ == "__main__":  
    my_list =[1, 1, 1, 5, 5, 3, 1, 3, 3, 1, 4, 4, 4, 2, 2, 2, 2] 
    
    CountFrequency(my_list)
    

    Reference:

    GeeksforGeeks

    0 讨论(0)
  • 2020-11-22 03:06

    i'm using Counter to generate a freq. dict from text file words in 1 line of code

    def _fileIndex(fh):
    ''' create a dict using Counter of a
    flat list of words (re.findall(re.compile(r"[a-zA-Z]+"), lines)) in (lines in file->for lines in fh)
    '''
    return Counter(
        [wrd.lower() for wrdList in
         [words for words in
          [re.findall(re.compile(r'[a-zA-Z]+'), lines) for lines in fh]]
         for wrd in wrdList])
    
    0 讨论(0)
  • 2020-11-22 03:06

    One more way is to use a dictionary and the list.count, below a naive way to do it.

    dicio = dict()
    
    a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
    
    b = list()
    
    c = list()
    
    for i in a:
    
       if i in dicio: continue 
    
       else:
    
          dicio[i] = a.count(i)
    
          b.append(a.count(i))
    
          c.append(i)
    
    print (b)
    
    print (c)
    
    0 讨论(0)
  • 2020-11-22 03:07

    Python 2.7+ introduces Dictionary Comprehension. Building the dictionary from the list will get you the count as well as get rid of duplicates.

    >>> a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
    >>> d = {x:a.count(x) for x in a}
    >>> d
    {1: 4, 2: 4, 3: 2, 4: 1, 5: 2}
    >>> a, b = d.keys(), d.values()
    >>> a
    [1, 2, 3, 4, 5]
    >>> b
    [4, 4, 2, 1, 2]
    
    0 讨论(0)
  • 2020-11-22 03:08

    You can do this:

    import numpy as np
    a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
    np.unique(a, return_counts=True)
    

    Output:

    (array([1, 2, 3, 4, 5]), array([4, 4, 2, 1, 2], dtype=int64))
    

    The first array is values, and the second array is the number of elements with these values.

    So If you want to get just array with the numbers you should use this:

    np.unique(a, return_counts=True)[1]
    
    0 讨论(0)
  • 2020-11-22 03:09

    This answer is more explicit

    a = [1,1,1,1,2,2,2,2,3,3,3,4,4]
    
    d = {}
    for item in a:
        if item in d:
            d[item] = d.get(item)+1
        else:
            d[item] = 1
    
    for k,v in d.items():
        print(str(k)+':'+str(v))
    
    # output
    #1:4
    #2:4
    #3:3
    #4:2
    
    #remove dups
    d = set(a)
    print(d)
    #{1, 2, 3, 4}
    
    0 讨论(0)
提交回复
热议问题