我下面有一个数组,其中包含重复的字符串。我想查找并替换那些字符串,但是每次匹配时,我都想更改替换字符串的值。
让我示范一下。
此样本数组:
SampleArray = ['champ', 'king', 'king', 'mak', 'mak', 'mak']
应该更改为:
SampleArray = ['champ', 'king1', 'king2', 'mak1', 'mak2', 'mak3']
如何做到这一点?我已经走了三天了,没有运气。提前致谢。
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')
www说
月关宝盒
相关分类