Change language

Python Counter Operations

|

# Write Python3 code here
# A counter is used in a string to find the frequencies of all its unique characters

 

from collections import Counter

s1 = ’aabbbcccdeff’

c1 = Counter (s1)

print ( "c1:" , c1)

 
# Counter used in the List to find the frequencies of all its unique list items

L1 = [ 1 , 2 , 1 , 1 , 4 , 4 , 4 , 5 , 6 , 6 , 3 , 3 , 0 , 0 ]

t1 = Counter (L1)

 

print ( "t1:" , t1)

Exit :

 c1: Counter ({’b’: 3,’ c’: 3, ’a’: 2, ’f’: 2,’ e’: 1, ’d’: 1}) t1: Counter ({1: 3, 4: 3, 0: 2, 3: 2, 6: 2, 2: 1, 5: 1}) 

Perform certain operations on the returned output dictionary if d is the output dictionary, for example d.items (), d.keys (), d .values ​​()

from collections import Counter

d = ’aabbbcccdeff’

d = Counter (d) 

 
# To print the counter value

print ( "d:" , d) 

  
# To access the values ​​corresponds to each key of the returned dictionary

print ( "d.values (): " , d.values ​​()) 

  
# Get keys and values ​​as from a dictionary

print ( "d.items ():" , d.items ())

 
# To get only keys

print   ( "d.keys ():" , d.keys ())

 
# Sort dictionary values ​​

print ( "sorted (d):" , sorted (d))

Exit :

 d: Counter ({’b’: 3,’ c’: 3, ’a’: 2, ’f’: 2,’ e’: 1, ’d’: 1}) d.values ​​(): dict_values ​​([2, 3, 3, 2, 1, 1]) d.items (): dict_items ([(’a’, 2), (’ b’, 3), (’c’, 3), (’ f’, 2), (’e’, 1), (’ d’, 1)] ) d.keys (): dict_keys ([’a’,’ b’, ’c’,’ f’, ’e’,’ d’]) sorted (d): [’a’,’ b’, ’ c’, ’d’,’ e’, ’f’] 

Adding two counters
Adding two counters creates the additions of the values ​​corresponding to each key, and if the key is present in one counter, and not in another, then in this case this value the key also ends up in the final output.

from collections import Counter

t1 = Counter ( ’aabbddffggjik’ )

t2 = Counter ( ’ aaabbbssshhhggdkkll’ )

 

print ( "t1:" , t1)

print ( "t2:" , t2)

print ( "t1 + t2:" , t1 + t2)

Exit:

 t1: Counter ({’g’: 2,’ a’: 2, ’b’: 2,’ f’: 2, ’d’: 2,’ k’: 1, ’j’: 1,’ i’: 1}) t2: Counter ({’a’: 3,’ b’: 3, ’h’: 3,’ s ’: 3,’ l’: 2, ’g’: 2,’ k’: 2, ’d’: 1}) t1 + t2: Counter ({’ a’: 5, ’b’: 5,’ g ’: 4,’ k’: 3, ’h’: 3,’ d’: 3, ’s’: 3,’ l’: 2, ’f’: 2,’ j’: 1, ’i’: 1}) 

Subtract two counts
The number of common items is subtracted from each other and (keeps only positive values)

from collections import Counter

t1 = Counter (  ’aabbddffggjik’ )

t2 = Counter ( ’aaabbbssshhhggdkkll’ )

 

print ( "t1:" , t1)

print ( " t2: " , t2)

print ( "t1-t2:" , t1 - t2)

print ( "t2-t1:" , t2 - t1)

 

Exit:

 t1: Counter ({’f’: 2,’ d’: 2, ’b’: 2,’ a’: 2, ’g’: 2,’ k’: 1, ’i’: 1,’ j’: 1}) t2: Counter ({’h’: 3,’ b’: 3, ’a’: 3, ’s’: 3,’ l’: 2, ’k’: 2,’ g’: 2, ’d’: 1}) t1-t2: Counter ({’ f’: 2, ’i’: 1, ’j’: 1,’ d’: 1}) t2-t1: Counter ({’h’: 3,’ s’: 3, ’l’: 2,’ k’: 1, ’b’: 1, ’a’: 1}) 

Intersections (& amp;) of two counters
Intersaction (& amp;) will only keep the minimum of the corresponding counts: min (t1 [x], t2 [x]):

from collections import Counter

t1 = Counter ( ’aaabbbbccdeeee’ )

t2 = Counter ( ’aabbccccdddee’ )

  

print ( "t1:" , t1)

print ( "t2:" , t2)

print ( "t1 & amp; t2:" , t1 & amp; t2)

Exit:

 t1: Counter ({’e’: 4,’ b’: 4 , ’a’: 3,’ c’: 2, ’d’: 1}) t2: Counter ({’ c’: 4, ’d’: 3,’ a’: 2, ’e’: 2,’ b’: 2}) t1 & amp; t2: Counter ({’c’: 2,’ a’: 2, ’e’: 2,’ b’: 2, ’d’: 1}) 

Union (|) of two counters
Union (|) will save only the maximum of the corresponding counts: max (c [x], d [x]):

Exit:

 t1: Counter ({’b’: 4,’ e’: 4, ’a’: 3,’ c’ : 2, ’d’: 1}) t2: Counter ({’ c’: 4, ’d’: 3,’ a’: 2, ’b’: 2,’ e’: 2}) t1 | t2: Counter ({’b’: 4,’ e’: 4, ’c’: 4,’ a’: 3, ’d’: 3}) 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically

from collections import Counter

t1 = Counter ( ’aaabbbbccdeeee’ )

t2 = Counter ( ’aabbccccdddee’ )

  

print ( "t1:" , t1)

print ( "t2:" , t2)

print ( "t1 | t2: " , t1 | t2)