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

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

    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)
    

提交回复
热议问题