{"id":6070,"date":"2023-10-29T10:40:23","date_gmt":"2023-10-29T05:10:23","guid":{"rendered":"https:\/\/python-programs.com\/?p=6070"},"modified":"2023-11-10T12:06:06","modified_gmt":"2023-11-10T06:36:06","slug":"find-frequency-of-each-character-in-string-and-their-indices-and-finding-duplicate-characters-in-a-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/find-frequency-of-each-character-in-string-and-their-indices-and-finding-duplicate-characters-in-a-string\/","title":{"rendered":"Find Frequency of Each Character in String and their Indices and Finding Duplicate Characters in a String"},"content":{"rendered":"
String:<\/strong><\/p>\n A string is a group of alphabets, words, or other characters. It is a primitive data structure that serves as the foundation for data manipulation. Python has a string class called str. Strings in Python are “immutable,” which means they can’t be modified once they’re formed. Because of the immutability of strings, we generate new strings as we go to represent computed values.<\/p>\n Given\u00a0 a string, the task is to find frequency of each character in a string .<\/p>\n Examples:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to find frequency of each character in a string some of them are:<\/p>\n Counter is a subclass of dict and a set. Counter() takes an iterable object as an argument and stores its elements as keys and their frequencies as values. As a result, if we transfer a string in collections. Counter() will then return a Counter class object with all characters in the string as keys and their frequency in the string as values.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We will construct a regex pattern to fit all of the alphanumeric characters in the string,<\/p>\n Make a Regex pattern that matches alphanumeric characters.<\/p>\n Iterate over all of the above-mentioned pattern matches in the string using pattern.<\/p>\n finditer()<\/strong> and generate dictionaries of each character’s frequency count and index position in the string.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Now, use collections to find all of the duplicate characters in this string. Counter() is used to determine the frequency of of character in a string, and characters with a frequency greater than 1 are considered duplicates.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n String: A string is a group of alphabets, words, or other characters. It is a primitive data structure that serves as the foundation for data manipulation. Python has a string class called str. Strings in Python are “immutable,” which means they can’t be modified once they’re formed. Because of the immutability of strings, we generate …<\/p>\nstring = \"hello this is btech geeks online learning platform for underguate students\"<\/pre>\n
Frequency of each character of the string is :\r\nThe frequency of character h is = 3\r\nThe frequency of character e is = 9\r\nThe frequency of character l is = 5\r\nThe frequency of character o is = 4\r\nThe frequency of character is = 10\r\nThe frequency of character t is = 6\r\nThe frequency of character i is = 4\r\nThe frequency of character s is = 5\r\nThe frequency of character b is = 1\r\nThe frequency of character c is = 1\r\nThe frequency of character g is = 3\r\nThe frequency of character k is = 1\r\nThe frequency of character n is = 6\r\nThe frequency of character a is = 3\r\nThe frequency of character r is = 4\r\nThe frequency of character p is = 1\r\nThe frequency of character f is = 2\r\nThe frequency of character m is = 1\r\nThe frequency of character u is = 3\r\nThe frequency of character d is = 2<\/pre>\n
Finding count of Each Character in a String and their Indices and Finding Duplicate Characters in a given String<\/h2>\n
\n
1)Using Counter() function\u00a0 to print frequency of each character in given string<\/h3>\n
# importing Counter function from collections\r\nfrom collections import Counter\r\n# Given string\r\nstring = \"hello this is btech geeks online learning platform for underguate students\"\r\n# Using counter() function to calculate frequency of each character of the string\r\nfreq = Counter(string)\r\nprint(\"Frequency of each character of the string is :\")\r\n# Traverse the freq dictionary and print their respective count\r\nfor key in freq:\r\n print(\"The frequency of character\", key, \"is =\", freq[key])\r\n<\/pre>\n
Frequency of each character of the string is :\r\nThe frequency of character h is = 3\r\nThe frequency of character e is = 9\r\nThe frequency of character l is = 5\r\nThe frequency of character o is = 4\r\nThe frequency of character is = 10\r\nThe frequency of character t is = 6\r\nThe frequency of character i is = 4\r\nThe frequency of character s is = 5\r\nThe frequency of character b is = 1\r\nThe frequency of character c is = 1\r\nThe frequency of character g is = 3\r\nThe frequency of character k is = 1\r\nThe frequency of character n is = 6\r\nThe frequency of character a is = 3\r\nThe frequency of character r is = 4\r\nThe frequency of character p is = 1\r\nThe frequency of character f is = 2\r\nThe frequency of character m is = 1\r\nThe frequency of character u is = 3\r\nThe frequency of character d is = 2<\/pre>\n
2)Using regex to find frequency and indices of all characters in a string<\/h3>\n
regex_Pattern = re.compile('[a-zA-Z0-9]')<\/code><\/p>\n
import re\r\n# Given string\r\nstring = \"hello this is btech geeks online learning platform for underguate students\"\r\n# regex pattern\r\nregex_Pattern = re.compile('[a-zA-Z0-9]')\r\n# Iterate through the string's alphanumeric characters which matches the regex pattern\r\n# While iterating, keep the frequency count of each character in a dictionary updated.\r\nmatchiterator = regex_Pattern.finditer(string)\r\ncharfrequency = {}\r\nindices = {}\r\nfor matchchar in matchiterator:\r\n charfrequency[matchchar.group()] = charfrequency.get(\r\n matchchar.group(), 0) + 1\r\n indices[matchchar.group()] = indices.get(\r\n matchchar.group(), []) + [matchchar.start()]\r\nprint(\"Frequency and indices of each character in the string is :\")\r\n# Traverse the charfrquency dictionary and print their respective count and indices\r\nfor key in charfrequency:\r\n print(\"The frequency of character\", key, \"is =\",\r\n charfrequency[key], \" ; Indices of occurrence = \", indices[key])\r\n<\/pre>\n
Frequency and indices of each character in the string is :\r\nThe frequency of character h is = 3 ; Indices of occurrence = [0, 7, 18]\r\nThe frequency of character e is = 9 ; Indices of occurrence = [1, 16, 21, 22, 31, 34, 58, 64, 70]\r\nThe frequency of character l is = 5 ; Indices of occurrence = [2, 3, 28, 33, 43]\r\nThe frequency of character o is = 4 ; Indices of occurrence = [4, 26, 47, 52]\r\nThe frequency of character t is = 6 ; Indices of occurrence = [6, 15, 45, 63, 67, 72]\r\nThe frequency of character i is = 4 ; Indices of occurrence = [8, 11, 29, 38]\r\nThe frequency of character s is = 5 ; Indices of occurrence = [9, 12, 24, 66, 73]\r\nThe frequency of character b is = 1 ; Indices of occurrence = [14]\r\nThe frequency of character c is = 1 ; Indices of occurrence = [17]\r\nThe frequency of character g is = 3 ; Indices of occurrence = [20, 40, 60]\r\nThe frequency of character k is = 1 ; Indices of occurrence = [23]\r\nThe frequency of character n is = 6 ; Indices of occurrence = [27, 30, 37, 39, 56, 71]\r\nThe frequency of character a is = 3 ; Indices of occurrence = [35, 44, 62]\r\nThe frequency of character r is = 4 ; Indices of occurrence = [36, 48, 53, 59]\r\nThe frequency of character p is = 1 ; Indices of occurrence = [42]\r\nThe frequency of character f is = 2 ; Indices of occurrence = [46, 51]\r\nThe frequency of character m is = 1 ; Indices of occurrence = [49]\r\nThe frequency of character u is = 3 ; Indices of occurrence = [55, 61, 68]\r\nThe frequency of character d is = 2 ; Indices of occurrence = [57, 69]<\/pre>\n
3)Using Counter to find Duplicate characters in the given string<\/h3>\n
# importing Counter function from collections\r\nfrom collections import Counter\r\n# Given string\r\nstring = \"hello this is btech geeks online learning platform for underguate students\"\r\n# Using counter() function to calculate frequency of each character of the string\r\nfreq = Counter(string)\r\nprint(\"Printing duplicate characters in the given string :\")\r\n# Traverse the freq dictionary and print the duplicate characters\r\nfor key in freq:\r\n # if the freq of character is greater than 1 then it is duplicate character so we print it\r\n if(freq[key] > 1):\r\n print(key)\r\n<\/pre>\n
Printing duplicate characters in the given string :\r\nh\r\ne\r\nl\r\no\r\n \r\nt\r\ni\r\ns\r\ng\r\nn\r\na\r\nr\r\nf\r\nu\r\nd<\/pre>\n
\nRelated Programs<\/strong>:<\/p>\n\n