0

小さなテキストを含むテキスト ファイルがあります。特定の単語 (「food」など) を検索できるようにし、直前の 5 文字を出力し、単語 (「food」) の出現回数の合計を出力する Python のスクリプトが必要です。終わり。

例:

「たくさんの食べ物を持っていく必要はありません。食べ物を詰める前に、ウィッシュリストを確認してください。すべての食べ物は到着時に検査されます。」

望ましい結果:

「の」、「あなたの」、「すべて」

3

どんな助けでも大歓迎です。

4

2 に答える 2

1

役立つ場合はこれを試してください。

>>> s = "You won't need to bring a lot of food with you. Before packing your food you should run through the wish list. All food will be inspected upon arrival."
>>> t = "food"
>>> s.split(t)
["You won't need to bring a lot of ", ' with you. Before packing your ', ' you should run through the wish list. All ', ' will be inspected upon arrival.']
>>> result = [part[-5:] for part in s.split(t)[:-1]]
>>> print result
['t of ', 'your ', ' All ']
>>> print len(result)
3
于 2013-09-15T13:27:26.790 に答える