【发布时间】:2018-03-04 11:56:06
【问题描述】:
如何解决这个重命名重复问题,而无需使用像 "_DUPLICATED_#NO" 这样的独特名称进行重命名,完成后名称必须是唯一的,并且最好使用表示重复数量的迭代数字
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']
如您所见,名称不是唯一的
【问题讨论】:
标签: python list duplicates rename