擅长:python、mysql、java
<p>要按排序顺序打印字符串中的重复字:</p>
<pre><code>from itertools import groupby
mysentence = ("As far as the laws of mathematics refer to reality "
"they are not certain as far as they are certain "
"they do not refer to reality")
words = mysentence.split() # get a list of whitespace-separated words
for word, duplicates in groupby(sorted(words)): # sort and group duplicates
count = len(list(duplicates)) # count how many times the word occurs
print('"{word}" is repeated {count} time{s}'.format(
word=word, count=count, s='s'*(count > 1)))
</code></pre>
<h3><a href="http://ideone.com/pXnRyP" rel="nofollow">Output</a></h3>
<pre>
"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
</pre>