Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python collections counter

import collections

arr = ['a', 'a', 'b', 'b', 'b', 'c']

# set the elements frequencies using Counter class
elements_count = collections.Counter(arr)

# printing the element and the frequency
for key, value in elements_count.items():
    print(f"{key}: {value}")

# output
# a: 2
# b: 3
# c: 1
Comment

collections counter

# importing the collections module
import collections
# intializing the arr
arr = [1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3]
# getting the elements frequencies using Counter class
elements_count = collections.Counter(arr)
# printing the element and the frequency
for key, value in elements_count.items():
   print(f"{key}: {value}")
Comment

python collections counter methods

c.total()                       # total of all counts
c.clear()                       # reset all counts
list(c)                         # list unique elements
set(c)                          # convert to a set
dict(c)                         # convert to a regular dictionary
c.items()                       # convert to a list of (elem, cnt) pairs
Counter(dict(list_of_pairs))    # convert from a list of (elem, cnt) pairs
c.most_common()[:-n-1:-1]       # n least common elements
+c                              # remove zero and negative counts
Comment

PREVIOUS NEXT
Code Example
Python :: Concatenating objects in pandas 
Python :: what is modulus in python 
Python :: check this id exist in database django 
Python :: maximize difference codechef 
Python :: subscriptable meaning in python 
Python :: how to get one record in django 
Python :: python iterating over a list 
Python :: create list of dictionaries from list of list python 
Python :: model coefficients 
Python :: maximum subarray sum 
Python :: python split range into n groups 
Python :: ssl socket python 
Python :: plotly pdf report 
Python :: python pickle dataframe 
Python :: parse xml in python 
Python :: how to address null in python 
Python :: block content 
Python :: merge pdf 
Python :: pandas remove multi header from dataframe 
Python :: invalid syntax python else 
Python :: python pass arguments in command line 
Python :: python ON DUPLICATE KEY UPDATE 
Python :: django strptime 
Python :: remove dict last element 
Python :: for in loop python 
Python :: librosa python 
Python :: dataframe, groupby, select one 
Python :: pandas heading 
Python :: expand pandas dataframe into separate rows 
Python :: python match case 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =