[PYTHON] Find out how many each character is in the string.

1


s = 'aeqwtwegwa'
d = {}

for c in s:
    if c not in d:
        d[c] = 0
    d[c] += 1

print(d)

Execution result of 1


{'a': 2, 'e': 2, 'q': 1, 'w': 3, 't': 1, 'g': 1}

2


s = 'aefqwdqfqwe'
d = {}

for c in s:
    d.setdefault(c, 0)
    d[c] += 1

print(d)

Execution result of 2


{'a': 1, 'e': 2, 'f': 2, 'q': 3, 'w': 2, 'd': 1}

If you read and write the defaultdict from the standard library,

3


from collections import defaultdict

s = 'raegiowejgohg'
d = defaultdict(int)

for c in s:
    d[c] += 1
    
print(d)
print(d['g'])

Execution result of 3


defaultdict(<class 'int'>, {'r': 1, 'a': 1, 'e': 2, 'g': 3, 'i': 1, 'o': 2, 'w': 1, 'j': 1, 'h': 1})
3

Recommended Posts

Find out how many each character is in the string.
Find out the apparent width of a string in python
[Golang] Check if a specific character string is included in the character string
How to input a character string in Python and output it as it is or in the opposite direction.
Check if the string is a number in python
How many bits is wchar_t? Wide character handling memo
[Python] How to expand variables in a character string
Find the part that is 575 from Wikipedia in Python
How to find out which process is using the localhost port and stop it
Maya | Find out the number of polygons in the selected object
I was in trouble because the character string in the PDF was strange
How to find out if there is an arbitrary value in "somewhere" of pandas DataFrame
Find the difference in Python
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
How to quickly count the frequency of appearance of characters from a character string in Python?
When a character string of a certain series is in the Key of the dictionary, the character string is converted to the Value of the dictionary.
In Django, how to abbreviate the long displayed string as ....
How to find the cumulative sum / sum for each group using DataFrame in Spark [Python version]
How to set the output resolution for each keyframe in Blender
How is the progress? Let's get on with the boom ?? in Python
[Introduction to Python] How to split a character string with the split function
How to give and what the constraints option in scipy.optimize.minimize is
[Introduction to Python] How to output a character string in a Print statement
Delete a particular character in Python if it is the last
How to use the asterisk (*) in Python. Maybe this is all? ..
How to extract the desired character string from a line 4 commands
The story that inheritance behavior is quite different in each language
Is it a character string operation?
Determine if the string is formatable
Get the query string (query string) in Django
How to find out the number of CPUs without using the sar command
[Introduction to Python] How to write a character string with the format function
Find out the name of the method that called it from the method that is python
[Introduction to Python] Thorough explanation of the character string type used in Python!
Find out where the java entity is on Linux (CentOS this time)
What's in the parameter? Edit String & Expression
[Python] How to invert a character string
Find out the CentOS7 MAC address (HWADDR)
What is "mahjong" in the Python library? ??
[Pandas] Expand the character string to DataFrame
When the target is Ubuntu 16.04 in Ansible
Escape curly braces in the format string
Find it in the procession and edit it
[PowerShell] Get the reading of the character string
How to use is and == in Python
Is there NaN in the pandas DataFrame?
The date is displayed incorrectly in matplotlib.
I want to visualize where and how many people are in the factory
How to find out what kind of files are stored in S3 in Python
Divides the character string by the specified number of characters. In Ruby and Python.
In Python, change the behavior of the method depending on how it is called
Why Docker is so popular. What is Docker in the first place? How to use
How to find the coefficient of the trendline that passes through the vertices in Python
[VBA] Function to judge whether the character string of UTF-8 file system is 255 or less and function to count UTF-8 character string in bytes