teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

コード変更

2021/06/08 01:37

投稿

WatanabeJin
WatanabeJin

スコア44

answer CHANGED
@@ -3,17 +3,13 @@
3
3
  [StackOverFlowの投稿](https://ja.stackoverflow.com/questions/77382/python%e3%81%ae%e3%83%aa%e3%82%b9%e3%83%88%e3%81%ab%e5%90%8c%e3%81%98%e4%b8%a6%e3%81%b3%e3%81%a7%e4%b8%a6%e3%82%93%e3%81%a7%e3%81%84%e3%82%8b%e8%a6%81%e7%b4%a0%e3%82%92%e8%a6%8b%e3%81%a4%e3%81%91%e3%81%a6%e7%b5%90%e5%90%88%e3%81%97%e3%81%9f%e3%81%84%e3%81%ae%e3%81%a7%e3%81%99%e3%81%8c%e3%82%88%e3%81%84%e6%96%b9%e6%b3%95%e3%82%92%e6%95%99%e3%81%88%e3%81%a6%e3%81%bb%e3%81%97%e3%81%84%e3%81%a7%e3%81%99)
4
4
 
5
5
  ```
6
+ def replace(word_list, s):
7
+ i = 0
8
+ s2 = ''.join(s)
6
- i, l = 0, len(word_list)-1
9
+ while i < len(word_list):
7
- while i < l:
8
- if word_list[i] =='焼肉' and word_list[i+1]=='定食':
10
+ if word_list[i] == s[0] and word_list[i:i+len(s)] == s:
9
- word_list[i:i+2] = ['焼肉定食']
11
+ word_list[i:i+len(s)] = [s2]
10
- l -= 1
11
- i += 1
12
+ i += 1
12
-
13
- print(word_list)
14
-
15
- # 実行結果
16
- ['東京', '人気', 'お店', 'メニュー', '焼肉定食', '300円', '期間', '限定', '焼肉', '単品', '250円', '焼肉', 'サラダ', '定食', '焼肉', '増量']
17
13
  ```
18
14
 
19
15
  皆様ありがとうございました。