この問題について正しく考えているかどうかはわかりません。重複のあるリストを取得し、反復サフィックスを追加してリストを「重複除去」する関数を書きたいと思います。
例えば:
dup_list = ['apple','banana','cherry','banana','cherry','orange','cherry']
復帰を目指して:
deduped = ['apple','banana1','cherry1','banana2','cherry2','orange','cherry3']
私の本能は、次のように、while ステートメントでリストを反復処理しながら pop 関数を使用することでした。
def dedup_suffix(an_list):
dedup=[]
for each in an_list:
an_list.pop(an_list.index(each)) #pop it out
i=1 #iterator
while each in an_list:
an_list.pop(an_list.index(each))
i+=1
appendage=str(each)+"_"+str(i)
else:
appendage=str(each)
dedup.append(appendage)
return dedup
しかし:
>>> dedup_suffix(dup_list)
['リンゴ'、'チェリー'、'オレンジ']
ポインタに感謝します。