我在下面有一个数组,它由重复的字符串组成。我想查找并替换这些字符串,但是每次进行匹配时,我都想更改替换字符串的值。
让我演示一下。
此示例数组:
SampleArray = ['champ', 'king', 'king', 'mak', 'mak', 'mak']
应该改为:
SampleArray = ['champ', 'king1', 'king2', 'mak1', 'mak2', 'mak3']
如何使这成为可能?我已经做了3天了,没有运气。提前致谢。
My Failed Code:
import os, collections, re
SampleArray = ['champ', 'king', 'king', 'mak', 'mak', 'mak']
dupes = [x for x, y in collections.Counter(SampleArray).items() if y > 1]
length = len(dupes)
count = 0
while count < length:
j = 0
instances = SampleArray.count(dupes[count])
while j < instances:
re.sub(dupes[count], dupes[count] + j, SampleArray, j)
j += 1
count += 1
print SampleArray
print ''; os.system('pause')