- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python - Odd Frequency Characters
In Python, fetching characters with odd frequencies from a given string can be a very common task in text processing and data analysis. In this article, we will learn various methods to fetch the odd frequency characters of a string in Python.
Using a Dictionary
Dictionaries are very convenient when there is a need to keep track of the frequencies of elements
Approach
To get the odd frequency elements, We will iterate through the entire string, and for each character in the string, we will increment the character count in the dictionary. At the end of the iteration, we will be left with a dictionary having the count of occurrences of each character, then we will display the characters having the odd frequency.
Explanation
Use a for loop to iterate through the string.
For each character update its count in the dictionary, with the key as the character.
Use the items() method to get the key, and value pairs from the dictionary and if the value is an odd number then append the character to the result list.
Example
st = "TutorialsPoint" Occurrences = {} result = [] # Populating dictionary with a count of each character for char in st: Occurrences[char] = Occurrences.get(char, 0) + 1 # fetching characters with odd frequency for char, count in Occurrences.items(): if count % 2 != 0: result.append(char) print(result)
Output
['T', 'u', 'r', 'a', 'l', 's', 'P', 'n']
Using a Counter
Python's Counter class from the collections module provides an easy way to count the frequency of elements in an iterable, including characters in a string. We can use this approach to simplify fetching characters with odd frequencies.
Syntax
Counter()
Counter(collection)
Collection: list, tuple, string, etc.
Explanation
Pass the string to the Counter()
The Counter() will return a dictionary object, which contains the frequency of each character in the list.
Iterate through the dictionary and append the characters having an odd frequency to the result list.
Example
from collections import Counter Occurrences = Counter("TutorialsPoint") result = [] for char, count in Occurrences.items(): if count % 2 != 0: result.append(char) print(result)
Output
['T', 'u', 'r', 'a', 'l', 's', 'P', 'n']
Using list comprehensions and the count() method
List comprehension provides a way to create lists based on existing lists or other iterables. we can use list comprehension along with the count method to count the frequency of each character in the string and construct a list of characters with odd frequencies.
Syntax
count()
count(element)
element :item we want to count in the iterable.
count() returns an integer, which is the count of the element.
Explanation
Iterate through the string using list comprehension
Get the list of unique characters in the string using set() method.
If the count of characters is an odd number then add it to the list.
Example
st = "TutorialsPoint" uniqueChars = set(st) result = [char for char in uniqueChars if st.count(char) % 2 != 0] print(result)
Output
['T', 'l', 'P', 'a', 'n', 's', 'r', 'u']
Using For Loop
We can use a nested loop to find the frequency of each character. For each character iterate through the entire list and increment the count value and if the count is odd add it to the result list.
Explanation
Iterate through each element in the list
For each element, check the occurrences of that element by iterating through the list.
If the count is odd, add the element to the result list.
Example 4
st = "TutorialsPoint" # getting distinct characters from string uniqueChars = set(st) result = [] for i in uniqueChars: count = 0 for j in st: if(i==j): count= count+1 if(count%2==1): result.append(i) print(result)
Output
['T', 's', 'P', 'r', 'n', 'a', 'u', 'l']
Conclusion
In this tutorial, we have discussed multiple approaches to retrieve odd frequency characters from a string. The last approach we implemented is not very efficient and can be only used for small sized data, whereas the other three approaches can be used for large sized inputs.