• Register
0 votes

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

Please log in or register to answer this question.

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")

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

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


11 6 4
34,950 points

Related questions

1 vote
1 answer 24 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, 2020 samhaz 5k points
0 votes
1 answer 40 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, 2020 LizzyM 6.1k points
0 votes
1 answer 2 views
Problem: I’m new in java. How do I count the number of occurrences of a char in a String? I have the string a.b.c.d I want to count the occurrences of '.' in an idiomatic way, preferably a one-liner. How to count it? Please help me to learn. Thanks.
asked Mar 15 Shovo210 13.9k points
1 vote
1 answer 29 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, 2020 Gavin 15.3k points
0 votes
2 answers 133 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 2 views
Problem: How to fix this error : python count occurrences of character in string
asked Mar 30 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 9 views
Problem: . Hi my friends i am begginer and i want to know how i do that please explain to me with example if you dont explain with example no explain please thank you
asked Feb 9 ahmedadel9090 3.1k points
1 vote
1 answer 114 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, 2020 samhaz 5k points
1 vote
1 answer 28 views
Problem: I come up with a silly question perhaps. The below program is printing a string within a single line. str = "Hello!" print(str) What if I want to print each and every single character in a different line from a string. I can guess an iteration ... process. How can I apply the iteration to print all the characters in different lines? I am looking forward to reading your answers. Thanks.
asked Jun 1, 2020 adamSw 11.3k points
1 vote
1 answer 2 views
Problem; Need help with the count of substring in Java. Count the number of occurrences of substring in a string in java.
asked Mar 29 PkGuy 13.1k points