回答編集履歴
2
追記
test
CHANGED
@@ -45,3 +45,25 @@
|
|
45
45
|
print(lis)
|
46
46
|
|
47
47
|
```
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
追記
|
52
|
+
|
53
|
+
---
|
54
|
+
|
55
|
+
こんなふうにも書けます。
|
56
|
+
|
57
|
+
```Python
|
58
|
+
|
59
|
+
work_set = set()
|
60
|
+
|
61
|
+
lis = [e + '_2' if e in work_set else work_set.add(e) or e for e in lis]
|
62
|
+
|
63
|
+
print(lis)
|
64
|
+
|
65
|
+
```
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
でもあんまり行儀の良いコードでは無いですね。避けた方が良いでしょう。
|
1
追記
test
CHANGED
@@ -17,3 +17,31 @@
|
|
17
17
|
print(lis)
|
18
18
|
|
19
19
|
```
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
lisがもっと長いなら、次のように別途記憶領域を用意した方が効率的です。
|
24
|
+
|
25
|
+
```Python
|
26
|
+
|
27
|
+
lis = ["hoge","hogehoge","fuga","fugafuga","hoge","fuga"]
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
appeared = set()
|
32
|
+
|
33
|
+
for i, e in enumerate(lis):
|
34
|
+
|
35
|
+
if e in appeared:
|
36
|
+
|
37
|
+
lis[i] += "_2"
|
38
|
+
|
39
|
+
else:
|
40
|
+
|
41
|
+
appeared.add(e)
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
print(lis)
|
46
|
+
|
47
|
+
```
|