I have a list of lists like this:
listofLists = [
['a', 'b', 'e'],
['a', 'd'],
['a', 'c'],
['a', 'b', 'c', 'e'],
['a', 'e', 'c', 'f']
]
How can i read this element list vertically and rename duplicates appending numbers to the element in incremental order? The output list has to preserve the same order.
For example, first position of 'e' is at index 1 in list ['a', 'e', 'c', 'f'], it should remain as it is. Next position of 'e' is at index 2 in list ['a', 'b', 'e'] which should be renamed as 'e1'. The last position of 'e' is in list ['a', 'b', 'c', 'e'] which should be renamed as 'e3'
The required output should be:
requiredOutput = [['a', 'b', 'e1'],
['a', 'd'],
['a', 'c'],
['a', 'b', 'c1', 'e2'],
['a', 'e', 'c1', 'f']]
The code i have tried is as below, by checking against a transposed list. But it does not give me the required output since my code changes the current element of the list. I dont know if this is the right approach to the problem statement.
transformedListOfTuples = list(zip_longest(*listofLists))
transformedListOfList= [list(x) for x in transformedListOfTuples]
for i in range(0, len(listofLists)):
for j in range(0, len(listofLists[i])):
pos = -1
for z in range(0, len(transformedListOfList)):
if listofLists[i][j] in transformedListOfList[z]:
pos = pos + 1
if pos == 0:
continue
elif pos > 0:
listofLists[i][j] = listofLists[i][j] + str(pos)
elif listofLists[i][j] not in transformedListOfList[z]:
continue