[Tutor] count words

Gregor Lingl glingl@aon.at
Thu Nov 14 01:35:02 2002


reavey schrieb:

>Sirs:
>I'd like to count the number of times  a letter occurs  in a string.
>I've been looking at c. It seems that this type of problem is being
>assigned in cs classes on graduate and advanced undergraduate classes in
>the US.
>Yet, I'm unable to find a library function or simply code written as a
>model to help me.
>
In Python strings and lists have a method count that may be used to 
solve sour problem:

 >>> "abaracadabara".count("a")
7
 >>> word = "abaracadabara"
 >>> word.count("r")
2
 >>> "one and two and three and four".split()
['one', 'and', 'two', 'and', 'three', 'and', 'four']
 >>> "one and two and three and four".split().count("and")
3
 >>> words = "one and two and three and four".split()
 >>> words
['one', 'and', 'two', 'and', 'three', 'and', 'four']
 >>> words.count("four")
1
 >>>

HTH Gregor

>Any pointer in the direction would be greatly appreciated.
>I would like to expand this model to include Dolce words, to help
>
                                               ?????                    
                                                         

>children with reading difficulties.
>TIA
>Re-v
>

>
>
>_______________________________________________
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>  
>