質問編集履歴
1
別案の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -56,4 +56,13 @@
|
|
56
56
|
|
57
57
|
### 補足情報(FW/ツールのバージョンなど)
|
58
58
|
|
59
|
-
python 3.6.x
|
59
|
+
python 3.6.x
|
60
|
+
|
61
|
+
### 追記
|
62
|
+
|
63
|
+
単語の順番を反転させずに済む方法も考えてみましたが、処理が遅いです。
|
64
|
+
```python
|
65
|
+
def remove_duplicate(txt):
|
66
|
+
unique_words = re.findall(r'\b(\w+)\b(?!.*\b\1\b)', txt) # ユニークな単語を抽出 (単語が最後に登場する順番)
|
67
|
+
return ' '.join(sorted(unique_words, key=lambda x: re.search(r'\b{}\b'.format(x), txt).start())) # 単語の最初の出現位置でソート
|
68
|
+
```
|