• Register
0 votes
22 views

How to get the number from the occurences of individual characters

Supose the string is:

x = 'cdfghcfonccn'

  How can i solve this problem.

10 7 2
6,060 points

1 Answer

0 votes

To solve your problem in a simple way you can use the code:

x = 'cdfghcfonccn'
dict((letter, x.count(letter)) for letter in set(x)

If you are using python 2.7 or python 3 you can use a tool named ‘Counter’:

import collections
results = collection.counter("cdfghcfonccn")
results

There is a tested method for counting this(python 3):

characlist = {}
for a in x:
    if a in characlist.keys():
         characlist [a]+=1
    else:
         characlist[a] = 1
print(characlist)

 

11 6 4
34,950 points

Related questions

1 vote
1 answer 17 views
17 views
Problem: I tried to count occurrences of individual character of string using python. So I wrote this program for the string: a = 'yanwawny' The program: ​​​string = 'yanwawny' ​dict((letter, string.count(letter)) for letter in set(a) But it throws the error message: File "main.py", line 1 ​a = 'yanwawny' ^ SyntaxError: invalid character in identifier What to do to solve this thing?
asked May 4 samhaz 5k points
0 votes
1 answer 19 views
19 views
Problem: I was trying to remove all occurrences of a character in a string python but I failed. The code I wrote is: def DeletChar(e, b) : counts = e.count(b) e = list(e) while counts : e.remove(b) counts -= 1 print(e) if __name__ == '__main__' : e = "geeksforgeeks" DeletChar(e,'g') The output ... e', 'k', 's', 'f', 'o', 'r', 'e', 'e', 'k', 's'] how to solve this problem I am a newbee please help
asked Mar 22 LizzyM 6.1k points
1 vote
1 answer 10 views
10 views
Problem: Hello All, I am a novice in Java programming. I am learning Java by taking an online class out of my own curiosity. I am solving basic level problems from many websites. So far, I was doing good. I get stuck as soon as I get a problem that asked ... characters in a string but how to solve this particular problem? Do you guys have any idea? Please, help me to solve the problem. Thanks.
asked Apr 14 Gavin 15.3k points
0 votes
2 answers 103 views
103 views
Problem : How to count the number of occurrences of a character in a string in java
asked Oct 23, 2019 peterlaw 6.9k points
1 vote
1 answer 47 views
47 views
Problem: I wrote this code to find all occurrences of a substring in a string python: import re test_str = "all in all is not the all of all " test_sub = "all" print("The main string is : " + test_str) print("The substring to find : " + test_sub) print(" ... is not defined The first two ouput of this code is right but it throws error message after that. What should I do to run this code properly?
asked Apr 22 samhaz 5k points