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

后端 未结 30 3497
时光说笑
时光说笑 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:04

    I would simply use scipy.stats.itemfreq in the following manner:

    from scipy.stats import itemfreq
    
    a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
    
    freq = itemfreq(a)
    
    a = freq[:,0]
    b = freq[:,1]
    

    you may check the documentation here: http://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.stats.itemfreq.html

提交回复
热议问题