fork download
  1. from itertools import groupby
  2.  
  3. mysentence = ("As far as the laws of mathematics refer to reality "
  4. "they are not certain as far as they are certain "
  5. "they do not refer to reality")
  6. words = mysentence.split() # get a list of whitespace-separated words
  7. for word, duplicates in groupby(sorted(words)):
  8. count = len(list(duplicates))
  9. print('"{word}" is repeated {count} time{s}'.format(
  10. word=word, count=count, s='s'*(count > 1)))
Success #stdin #stdout 0.1s 10104KB
stdin
Standard input is empty
stdout
"As" is repeated 1 time
"are" is repeated 2 times
"as" is repeated 3 times
"certain" is repeated 2 times
"do" is repeated 1 time
"far" is repeated 2 times
"laws" is repeated 1 time
"mathematics" is repeated 1 time
"not" is repeated 2 times
"of" is repeated 1 time
"reality" is repeated 2 times
"refer" is repeated 2 times
"the" is repeated 1 time
"they" is repeated 3 times
"to" is repeated 2 times