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 :: check all python versions windows 
Python :: how to lock writing to a variable thread python 
Python :: minimum from list of tuples 
Python :: how to extract words from sentence in python 
Python :: how to download python freegames 
Python :: pandast change datetime to date 
Python :: numpy identity matrix 
Python :: python sort list in reverse order 
Python :: how to ascess GPS in python 
Python :: rename file python 
Python :: pandas replace values in column based on condition 
Python :: orderd dictionary pop vs del 
Python :: gonad 
Python :: get datafram colum names as list python 
Python :: python extract all numbers from string re 
Python :: python record screen 
Python :: how to fill an array with consecutive numbers 
Python :: python how to obfuscate code 
Python :: how to print a line letter by letter in python 
Python :: how to import PyMem python 
Python :: user input dictionary python 
Python :: pandas add a column with loc 
Python :: dataframe index rename 
Python :: none address in python 
Python :: python read text file into a list 
Python :: plt.savefig without showing 
Python :: python edit text file 
Python :: dataframe auto detect data types 
Python :: python print time difference 
Python :: save matplotlib figure 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =