表1-4に一覧されている演算子を利用する。
>>> [w for w in text6 if w.endswith('ize')]
[]
>>> [w for w in text6 if 'z' in w]
['zone', 'amazes', 'Fetchez', 'Fetchez', 'zoop', 'zoo', 'zhiv', 'frozen', 'zoosh']
>>> [w for w in text6 if 'pt' in w]
['empty', 'aptly', 'Thpppppt', 'Thppt', 'Thppt', 'empty', 'Thppppt', 'temptress', 'temptation', 'ptoo', 'Chapter', 'excepting', 'Thpppt']
>>> [w for w in text6 if w.istitle()]
(省略)
リストを定義し、それに対して条件分岐処理を行う。ize で終わる単語は無いらしい。
>>> shtext = ['she', 'sells', 'sea', 'shells', 'by', 'the', 'sea', 'shore']
>>> [w for w in shtext if w.startswith('sh')]
['she', 'shells', 'shore']
>>> [w for w in shtext if len(w) > 4]
['sells', 'shells', 'shore']
sum([len(w) for w in text1]) は、単語の長さの合計を示す。 よって、単語数で割ることで、平均単語長を計算できる。 割り算の前に浮動小数点にしておく。
>>> sum([len(w) for w in text1])
999044
>>> len(text1)
260819
>>> float(sum([len(w) for w in text1])) / float(len(text1))
3.8304111280236488