About the usefulness of the Python Counter class-You don't have to count it yourself anymore-

Overview

Until now, in order to check how many elements are included in the list, the list element was rotated by the for statement and the combination of the element and the number was set in the dict. However, if you use the collections.Counter class, you can process it in one line.

So far (dirty with dict)

    a = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
    b = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6]

    a_dict = dict()
    b_dict = dict()

    for item in a:
        if item in a_dict:
            a_dict[item] += 1
        else:
            a_dict[item] = 1

    for item in b:
        if item in b_dict:
            b_dict[item] += 1
        else:
            b_dict[item] = 1

    print(a_dict) #{1: 1, 2: 2, 3: 3, 4: 4, 5: 5}
    print(b_dict) #{1: 1, 2: 2, 3: 3, 4: 4, 6: 6}

With the Counter class

    from collections import Counter

    a = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
    b = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6]

    a_counter = Counter(a)
    b_counter = Counter(b)
    print(a_counter) #Counter({5: 5, 4: 4, 3: 3, 2: 2, 1: 1})
    print(b_counter) #Counter({6: 6, 4: 4, 3: 3, 2: 2, 1: 1})

In the Counter class, union and difference sets can be calculated.


    from collections import Counter

    a = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
    b = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6]

    a_counter = Counter(a)
    b_counter = Counter(b)

    print(a_counter + b_counter) #Counter({4: 8, 3: 6, 6: 6, 5: 5, 2: 4, 1: 2})
    print(a_counter - b_counter) #Counter({5: 5})

Problems that can be easily solved by using the Counter class in AtCoder

Recommended Posts

About the usefulness of the Python Counter class-You don't have to count it yourself anymore-
About the ease of Python
About the features of Python
Convert the result of python optparse to dict and utilize it
About the basics list of Python basics
Have Alexa run Python to give you a sense of the future
I don't want to admit it ... The dynamical representation of Neural Networks
About the virtual environment of python version 3.7
About the handling of ZIP files including Japanese files when upgrading from Python2 to Python3
The math of some entrance exam question is awkward to think about, so I left it to python after all
A note about the python version of python virtualenv
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
About the * (asterisk) argument of python (and itertools.starmap)
I used Python to find out about the role choices of the 51 "Yachts" in the world.
How to count the number of occurrences of each element in the list in Python with weight
A story about trying to introduce Linter in the middle of a Python (Flask) project
Don't take an instance of a Python exception class directly as an argument to the exception class!
[Python] About creating a tool to display all the pages of the website registered in the JSON file & where it got caught
python beginners tried to predict the number of criminals
The wall of changing the Django service from Python 2.7 to Python 3
Think about how to program Python on the iPad
Template of python script to read the contents of the file
How to get the number of digits in Python
[python] option to turn off the output of click.progressbar
[Python] Summary of how to specify the color of the figure
14 quizzes to understand the surprisingly confusing scope of Python
A reminder about the implementation of recommendations in Python
[Introduction to Python] Basic usage of the library matplotlib
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
The attitude that programmers should have (The Zen of Python)
Completely translated the site of "The Hitchhiker's Guide to Python"
Python Note: The mystery of assigning a variable to a variable
I tried to summarize the string operations of Python
Use hash to lighten collision detection of about 1000 balls in Python (related to the new coronavirus)
How to quickly count the frequency of appearance of characters from a character string in Python?
Return the image data with Flask of Python and draw it to the canvas element of HTML
I made a function to crop the image of python openCV, so please use it.
I don't have a sense of "quiz asking investment sense", so I tried to solve it with brute force (Python Monte Carlo simulation)