1
如何解决这一更名,而不诉诸具有独特的像"_DUPLICATED_#NO"
名字的东西重命名重复的问题必须在完成时是唯一的,最好用迭代数字表示重复的数量Python的重命名复制
from collections import defaultdict
l = ["hello1","hello2","hello3",
"hello","hello","hello"]
tally = defaultdict(lambda:-1)
for i in range(len(l)):
e = l[i]
tally[e] += 1
if tally[e] > 0:
e += str(tally[e])
l[i] = e
print (l)
结果:
['hello1', 'hello2', 'hello3', 'hello', 'hello1', 'hello2']
,你可以看到,该名称不是唯一的
@PRMoureu固定。哎呀,算法很难;)这将'''hello1','hello1']'变成'['hello1','hello11']',但我想不出一个好的方法来概括一个解决方案,产生'['hello1','hello2']'的方式不会破坏其他不太明显的边缘情况。 –
这是好的,做得好,没有想到使用while while _ _ – citizen2077
@new_to_coding检查我的编辑,如果你使用它来创建文件。 –