
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generate Two Output Strings Based on Character Occurrence in Python
In this program , we take a string and count the characters in it with certain condition. The first condition is to capture all those characters which occur only once and the second condition is to capture all the characters which occur more than once. Then we list them out.
Below is the logical steps we are going to follow to get this result.
- Counter converts the strings into Dictionary which is having keys and value.
- Then separate list of characters occurring once and occurring more than once using the join()
In the below program we take the input string and
Example
from collections import Counter def Inputstrings(load): Dict = Counter(load) occurrence = [key for (key, value) in Dict.items() if value == 1] occurrence_1 = [key for (key, value) in Dict.items() if value > 1] occurrence.sort() occurrence_1.sort() print('characters occurring once:') print(''.join(occurrence)) print('characters occurring more than once:') print(''.join(occurrence_1)) if __name__ == "__main__": load = "Tutorialspoint has best tutorials" Inputstrings(load)
Running the above code gives us the following result −
Output
characters occurring once: Tbehnp characters occurring more than once: ailorstu
Advertisements