LoginSignup
5
4

More than 5 years have passed since last update.

素人の言語処理100本ノック:03

Last updated at Posted at 2016-09-11

言語処理100本ノック 2015の挑戦記録です。環境はUbuntu 16.04 LTS + Python 3.5.2 :: Anaconda 4.1.1 (64-bit)です。過去のノックの一覧はこちらからどうぞ。

第1章: 準備運動

03.円周率

"Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."という文を単語に分解し,各単語の(アルファベットの)文字数を先頭から出現順に並べたリストを作成せよ.

出来上がったコード:

main.py
# coding: utf-8
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)

実行結果:

端末
[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]

,.の扱いがあんまり良くない感じがしています。str.split()がデリミタ文字列を複数指定できるときれいでいいのですが。


(2016/09/11追記)
shiracamusさんよりアドバイス頂きました。ありがとうございます!
問題が「アルファベットの文字」なので、アルファベットのみカウントするようにしてみました。

main2.py
# coding: utf-8
target = 'Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.'
result = []

words = target.split(' ')
for word in words:
    count = 0
    for char in word:
        if char.isalpha():
            count += 1
    result.append(count)

print(result)

先輩方のコードを拝見すると、内包表記を使うことでもっと効率的に書けそうです。ただ、不慣れでパッと見では理解しにくいので、慣れるまでは冗長なままでいこうかと思います^^;
 
4本目のノックは以上です。誤りなどありましたら、ご指摘いただけますと幸いです。

5
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4