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

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

提交回复
热议问题