I have the following input:
'lesson*lesson*practice*lesson*quiz*practice'
and here's the desired output:
'lesson1*lesson2*practice1*lesson3*quiz*practice2'
So if any item repeats in the list, I'd like to append 1,2,3... to the occurrences. To get this done, I came up with the following code:
line = 'lesson*lesson*practice*lesson*quiz*practice'
line1 = line
def list_to_dict(li):
dct = {}
for item in li.split('*'):
if dct.has_key(item):
dct[item] = dct[item] + 1
else:
dct[item] = 1
return dct
for item in list_to_dict(line).keys():
counter = 1
if list_to_dict(line)[item] > 1:
indices = [i for i, x in enumerate(line.split('*')) if x == item]
for idx, _ in enumerate(line1[indices]):
line1[idx] = line1[idx] + str(counter)
counter = counter + 1
print line1
print line
But this code gives me the following error:
TypeError Traceback (most recent call last)
<ipython-input-187-a85290a89eea> in <module>()
6 if list_to_dict(line)[item] > 1:
7 indices = [i for i, x in enumerate(line) if x == item]
----> 8 for idx, _ in enumerate(line1[indices]):
9 line1[idx] = line1[idx] + str(counter)
10 counter = counter + 1
TypeError: string indices must be integers, not list
Would someone please be able to help get the desired output?
If you think my algorithm to achieve this is not good, I'd welcome any new ideas, too.