今という器の中から

過ごした時間を振り返る雑記ブログ

pythonを使った、英文を分解し,アルファベットの文字数を取得したリストの作成の学習メモ

sentence = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
count = [len(word.strip(".,")) for word in sentence.split()]
print(count)

言語処理100本ノック 第1章 in Python - Qiita

 
sentence="Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
該当の文字列を sentence に格納する

count=[len(word.strip(".,")) for word in sentence.split()]

count= count に [len(word.strip(".,")) for word in sentence.split()] を格納する。
[len(word.strip(".,")) for word in sentence.split()] とは

内包表記としてリストの作成
単語の文字数を取得したリストの作成

len() とは
単語の文字数を取得する
word.strip(".,") の文字数を取得する

.strip(".,") とは
word から . , を取り除く

word とは
.split() された sentence ≒ 単語

.split() とは
スペース、タブ、改行文字ごとに分割する
文章を単語ごとに区切る

sentence とは
Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.
print(count)

count を出力する