前提・実現したいこと
#入力 It’s a piece of cake That’s all right #出力 It is a piece of cake That is all right
上記のように、アポストロフィーのある文を分解した形で出力しようとしています。
発生している問題・エラーメッセージ
現在のコードだと、辞書の内容が反映されず、配列の中身の取り出し方にも問題があるようで修正方法がわからない状態です。
['It’s a piece of cake'] ['It’s a piece of cake'] ['That’s all right'] ['That’s all right']
該当のソースコード
python
1contractions = {"It’s":"It is", "I’d": "I would", "That’s": "That is"} 2texts = [['It’s a piece of cake'], 3 ['That’s all right']] 4for i in range(len(texts)): 5 text = str(texts[i]) 6 for word in text.split(): 7 print(word) 8 if word in contractions: 9 text = text.replace(word, contractions[word]) 10 print(text)
試したこと
入力データの配列構造をやめてもアポストロフィーの分解には失敗し、分解されないままの出力となりました。
contractions = {"It’s":"It is", "I’d": "I would", "That’s": "That is"} word = 'It’s a piece of cake' for word in text.split(): print(word) if word in contractions: text = text.replace(word, contractions[word]) print(text) #出力 It’s a piece of cake
補足情報(FW/ツールのバージョンなど)
Python 3.7
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。