エンジニアのサブルーチン

あまり注目されない組み込みのシステムエンジニアをやっています。エンジニア業界のこととか、勉強したこととかを不定期に更新していきます。

Python100本ノック 3問目

f:id:code_sugar:20190823001856p:plain

split(' ')したのはいいものの ,や.が含まれてるから、これをどう処理したらいいかわからなかった。

解答

target = 'Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.'
result = []

words = target.split(' ')
for word in words:
    result.append(len(word) - word.count(',') - word.count('.'))

print(result)


各メソッドの解説

append()

append()で配列に新しく要素を追加します

list = []
list.append('test1')  # ==>['test1']
list.append('test2') # ==>['test1', 'test2']

count()

指定した引数を探し出して数えてくれる。

...

ちょっと俺的にはスッキリしない解答です(もっとスマートな解答を求めていた)