groupby を使用して単語のリストを解析し、それらを長さごとにリストに整理しています。例えば:
from itertools import groupby
words = ['this', 'that', 'them', 'who', 'what', 'where', 'whyfore']
for key, group in groupby(sorted(words, key = len), len):
print key, list(group)
3 ['who']
4 ['this', 'that', 'them', 'what']
5 ['where']
7 ['whyfore']
リストの長さを取得することも同様に機能します。
for key, group in groupby(sorted(words, key = len), len):
print len(list(group))
1
4
1
1
このように事前に条件を付けると、結果は次のようになります。
for key, group in groupby(sorted(words, key = len), len):
if len(list(group)) > 1:
print list(group)
出力:
[]
どうしてこれなの?