How to quickly count the frequency of appearance of characters from a character string in Python?

word = 'adsgsdfhsdgfhsdfggsfdgsdfgs'

Well, if there is a character string like this, I want to count the frequency of appearance of characters, but I do not want to do anything complicated, so I thought about how to do it.

#Make a set of characters from a string
char_set = { x for x in word }

#Represent the frequency of appearance in a dictionary
char_freq = { x : word.count(x) for x in char_set }

I think that nltk is necessary to calculate the frequency of appearance of character strings in a more serious manner, but even with this, I felt that it would fall into my mind. Good.


Postscript

It seems that the Counter object added in Python 2.7 is just right. yasuharu519 Thank you for your information.

from collections import Counter

c = Counter(word)

Recommended Posts

How to quickly count the frequency of appearance of characters from a character string in Python?
[Python] How to expand variables in a character string
How to identify the element with the smallest number of characters in a Python list?
# 5 [python3] Extract characters from a character string
[Python] How to invert a character string
How to determine the existence of a selenium element in Python
[Introduction to Python] How to split a character string with the split function
[Introduction to Python] How to output a character string in a Print statement
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
How to extract the desired character string from a line 4 commands
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
Fill the string with zeros in python and count some characters from the string
[Introduction to Python] How to write a character string with the format function
[Introduction to Python] Thorough explanation of the character string type used in Python!
How to pass the execution result of a shell command in a list in Python
How to convert / restore a string with [] in python
How to get the number of digits in Python
Divides the character string by the specified number of characters. In Ruby and Python.
How to count the number of elements in Django and output to a template
How to input a character string in Python and output it as it is or in the opposite direction.
How to get a list of files in the same directory with python
How to check in Python if one of the elements of a list is in another list
How to count the number of occurrences of each element in the list in Python with weight
Find out the apparent width of a string in python
[Python] How to make a list of character strings character by character
How to slice a block multiple array from a multiple array in Python
How to use the __call__ method in a Python class
How to develop in a virtual environment of Python [Memo]
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
Python learning basics ~ How to output (display) a character string? ~
How to connect the contents of a list into a string
"A book to train programming skills to fight in the world" Python code answer example --1.2 Count the number of the same characters
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.
How to plot the distribution of bacterial composition from Qiime2 analysis data in a box plot
How to pass the execution result of a shell command in a list in Python (non-blocking version)
Create an instance of a predefined class from a string in Python
Count the number of Thai and Arabic characters well in Python
How to know the internal structure of an object in Python
How to make a string into an array or an array into a string in Python
Try to extract a character string from an image with Python3
[Introduction to Python] How to use the in operator in a for statement?
How to get the vertex coordinates of a feature in ArcPy
How to get a stacktrace in python
Generate a class from a string in Python
How to take a screenshot of the Chrome screen (prevent it from cutting off in the middle)
Summary of character string format in Python3 Whether to live with the old model or the new model
I want to extract an arbitrary URL from the character string of the html source with python
How to send a visualization image of data created in Python to Typetalk
[Python] How to put any number of standard inputs in a list
[Introduction to Python] How to sort the contents of a list efficiently with list sort
How to get a value from a parameter store in lambda (using python)
I want to batch convert the result of "string" .split () in Python
I want to color a part of an Excel string in Python
How to write a string when there are multiple lines in python
How to format a list of dictionaries (or instances) well in Python
I made a program to check the size of a file in Python
How to sort by specifying a column in the Python Numpy array.
"A book to train programming skills to fight in the world" Python code answer example --1.1 Duplicate character string
4 methods to count the number of occurrences of integers in a certain interval (including imos method) [Python implementation]