• Register
0 votes
26 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

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")
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 19 views
19 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 32 views
32 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
1 vote
1 answer 18 views
18 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 116 views
116 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 82 views
82 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 17 views
17 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 19 views
19 views
Problem: For the following string I tried to convert is’s first character to a upper case. So I write a program. The string: forgodsake The program I wrote is: main_str = "forgodsake" print("The main string youput is : " + str(main_str)) key = main_str[0] + ... I am getting is not right. What is the problem in my code, why am I not getting the expected result. Can anyone sort this out?
asked Jun 10, 2020 samhaz 5k points
0 votes
1 answer 21 views
21 views
Problem: Hello kodlogs, Recently while attempting, the problem in the python there is a error that came can you please help sort out this problem, How can I get the first character from the first string in Python?,It seems that I could use list[0][1:] but that does not give me the first character of the string that I am looking for. mysrt = "sdf" >>> mystr[0][1:]
asked Jun 6, 2020 Gavin 15.3k points
2 votes
1 answer 19 views
19 views
Problem: Hello all, I am a new student in the Python programming language. I am learning through a textbook. I just completed the String chapter and started solving the problems of this chapter. Some of the problems are easy to solve for me. I stuck on a point ... understand. I’ve seen some of the solutions answered by the good people. If anybody could help me would be great for me. Thanks
asked Mar 29, 2020 Gavin 15.3k points
0 votes
1 answer 12 views
12 views
I am new. Please help me. solve this problem.
asked Nov 17, 2020 TeamScript 9.5k points