Hack Forums
[Python] occurrence of all char in the string - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] occurrence of all char in the string (/showthread.php?tid=6132799)



occurrence of all char in the string - blackcoder00 - Jan 10, 2021

i have some problem when i wanna get the occurrence of all char in the string
output :
Code
donner ch
aamiinee
*******
le caractaire : a  :  1
*******
le caractaire : a  :  1
*******
le caractaire : m  :  1
*******
le caractaire : i  :  2
*******
le caractaire : i  :  2
*******
le caractaire : n  :  1
*******
le caractaire : e  :  1
*******
le caractaire : e  :  1

PHP Code

<?php 
def occurence
(ch):
    
nb=0
    
for i in range (len(ch)):
        print(
"*******")
        
x=ch[i]
        for 
j in range (1,len(ch)-1):
            if 
ch[j]==x:
                
nb=nb+1
        
print("le caractaire :",x," : ",nb)
        
nb=0
ch
=input("donner ch \n")
print(
occurence(ch)) 



RE: occurrence of all char in the string - HFDone - Jan 10, 2021

Code
a = 'have a nice day'
symbol = 'abcdefghijklmnopqrstuvwxyz'
for key in symbol:
    print key, a.count(key)

function

def check(str):
    freq = {}
    for c in str:
      freq[c] = str.count(c)
    print freq



RE: occurrence of all char in the string - Arrogance - Jan 12, 2021

More pythonic:
Code
import collections
s = "this is a string"
counter = collections.Counter(s)
for char, freq in counter.items():
    print(char, freq)