This is what I have: A list which contains sublists
A= [['filename.yaml','0001'],['filename.yaml','0001'],['filename.yaml','0001'], ['fname.yaml','0002'], ['fname.yaml','0002']]
What i want is to rename the first element of each sublist when the sublist is present more than once. The out put should be:
[['filename_0.yaml','0001'],['filename_1.yaml','0001'],['filename_2.yaml','0001'], ['fname_0.yaml','0002'], ['fname_1.yaml','0002']]
This is my code:
def asso_name_id(A):
for sublist in A:
if A.count(sublist)>1:
for i in range(A.count(sublist)):
base=os.path.splitext(os.path.basename(sublist[0]))[0]
sublist[0]=base+"_"+str(i)+'.yaml'
This is what i get with this code:
[['filename_0_1_2.yaml', '0001'], ['filename_0_1.yaml', '0001'], ['filename.yaml', '0001'], ['fname_0_1.yaml', '0002'], ['fname.yaml', '0002']]
What am I doing wrong and how can I fix it?