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

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

    You can use the in-built function provided in python

    l.count(l[i])
    
    
      d=[]
      for i in range(len(l)):
            if l[i] not in d:
                 d.append(l[i])
                 print(l.count(l[i])
    

    The above code automatically removes duplicates in a list and also prints the frequency of each element in original list and the list without duplicates.

    Two birds for one shot ! X D

提交回复
热议问题