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 :: ImportError: No module named flask 
Python :: python try catch 
Python :: 2 variables with statement python 
Python :: how to fill a list in python 
Python :: SQLAlchemy query to dict 
Python :: add age categories pandas dataframe 
Python :: pre commit python 
Python :: find most frequent element in an array python 
Python :: remove columns from a dataframe python 
Python :: download images python google 
Python :: python numpy array to list 
Python :: when was python developed 
Python :: python read parquet 
Python :: pandas sort by date descending 
Python :: python check if string is in input 
Python :: python list empty 
Python :: django ckeditor not working 
Python :: python decimal to string 
Python :: how to close windows in selenium python without quitting the browser 
Python :: python simple input popup 
Python :: how to get input from list in python 
Python :: date object into date format python 
Python :: redirect if not logged in django 
Python :: django template for loop 
Python :: basic calculator in python 
Python :: Issue TypeError: can’t multiply sequence by non-int of type str 
Python :: jaccard distance python 
Python :: integer colomn to datetime 
Python :: select 2 cols from dataframe python pandas 
Python :: python merge two dictionaries in a single expression 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =