The statistics python string of letters occurrences

dic=dict()
d={}
s=set()
s='helloworld'
(1)d=dict()
    for x in s:
      if x not in d.keys():
          d[x]=1
       else:
         d[x]=d[x]+1
    print(d)

(2)d2=dict()
    for x in s:
      d2[x]=d2.get(x,0)+1
    print(d2)

(3)d3=dict()
   for x in s:
      d3[x]=s.count(x)
   print(d3)

 

 

 The above gives a total of three methods, it is based on the output of the dictionary form, but can be seen more easily by the method of the second three kinds of built-in functions

DEF countchar (STR): 

    STR = str.lower () # Chemical lowercase 

    ANS = [] 

    for I in Range (26 is): # listing initial values 26 0 

        ans.append (0) 

    for I in STR: 

        IF (the ord (I)> = the ord ( ' A ' ) and the ord (I) <= the ord ( ' Z ' )): 

            ANS [the ord (I) -ORd ( ' A ' )] = ANS [the ord (I) -ORd ( ' A ' )] +. 1   # count the number of 

    return ANS 

IF the __name__ == " __main__ " : 

     STR = INPUT () 

     Print (countchar (STR)) 


DEF countchar (ST):                         # number defines the number of function 
    
    Keys = [CHR (97 + I) for I in Range (26 is)] # generating a list of key 26 letters 
    
    DI = dict (). fromkeys (Keys, 0)           # assigned to each key initial value 0 
    
    new new = []                   # create a new list for storing ordered key 
    
    ST = st.lower ( )            # all characters to lowercase input 
    
    for S inST:               # Traversal string 
   
            di [S] = st.count (S) # output number of each character stored in the dictionary 

        for K in Keys:         # traversal keys, adding its value to the new list di the number 26 is obtained ordered letters 

            new.append (DI [K]) 

        return new new             # return there alphabetical list number 26 

IF  the __name__ == " __main__ " : 

    ST = iNPUT ()               # input string 

    str1 = ""                  # define an empty string 

    for S in ST:               # traversing input string 

        ifs.isalpha () = 0:!   # only was added to the new string of letters, punctuation negligible 
  
            str1 + = S 
    
    Print (countchar (str1))     # Output List

The above two methods are the letters of the output string occurrence frequency is slightly different, where it is set to 26 letters, and make the corresponding initial value is 0, then the string of individual letters statistical occurrences each letter appears a number of times, that is at the initial value to its corresponding. And the letter does not appear, the value corresponding to the initial value remains 0

By several methods described above, we can summarize the ideas to solve this problem: From the keyboard enter a random string, and then loop through the string, string loop through each character, count the number of kinds of characters appear , looping through the string

Guess you like

Origin www.cnblogs.com/jackyfive/p/11783105.html
Recommended