以下のような処理をしたいのですが、うまいやり方がわかりません。
例:
(str)'This is not a pen, is it?'
->
(list)['This','is','not','a','pen',',','is','it','?']
無理矢理以下のような書き方をしてみましたが、かなり無駄な書き方をしているように感じます。。
スッキリした書き方を教えて戴けないでしょうか?
すみませんが、宜しくお願いします!
python
1script = "This is not a pen, is it?" 2split_script = script.split(' ') 3ans = [] 4for word in split_script: 5 if ',' in word: 6 word_tup = word.partition(',') 7 ans.append(word_tup[0]) 8 ans.append(word_tup[1]) 9 elif '?' in word: 10 word_tup = word.partition('?') 11 ans.append(word_tup[0]) 12 ans.append(word_tup[1]) 13 else: 14 ans.append(word) 15print('ans = ',ans)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/05 14:47