Reputation: 501
I have one list which contains duplicate values. I want to keep duplicates values by adding a char 'v1' or v2.. so on. The counter will increase if the particular number is repeated as many times
mk = ['1','1','2','3','2','4','4','4','5']
The output will look like this.
['1','1_v1','2','2_v1','3','4','4_v1','4_v2','5']
Upvotes: 1
Views: 335
Reputation: 180
Create a dictionary to maintain the frequency of occurrence of elements and then use the dictionary to append values to the elements.
mk = ['1','1','2','3','2','4','4','4','5']
my_dict = {key:0 for key in mk}
for i in range(len(mk)):
my_dict[mk[i]] += 1 #increase the occurrence count
if my_dict[mk[i]] > 1: #check if the element occurred more than once
mk[i] = mk[i]+"_v"+str(my_dict[mk[i]] - 1) #if yes append the value
Upvotes: 1