擅长:python、mysql、java
<pre><code>from collections import Counter
Data = 'Did the quick brown fox really jump over the fence'
Freq = Counter([len(words) for words in Data.split()])
print ([Freq[Num] if Num in Freq else 0 for Num in range(1, max(Freq)+1) ])
</code></pre>
<p><strong>输出</strong></p>
<pre><code>[0, 0, 4, 2, 3, 1]
</code></pre>
<p><strong>易于理解的版本</strong></p>
<pre><code>from collections import Counter
Data = 'Did the quick brown fox really jump over the fence'
Freq = Counter([len(words) for words in Data.split()])
Result = []
for Num in range(1, max(Freq)+1):
if Num in Freq:
Result.append(Freq[Num])
else:
Result.append(0)
print (Result)
</code></pre>