1

I need to indexing a list of string because I don't want duplicates to avoid the overwritting when I rename files.

Let's say that I have a liste like this one :

list = ['name', 'city', 'city', 'name1', 'town', 'city2', 'town', 'name2']

And I would like to get a list like this one :

list = ['name3', 'city1', 'city3', 'name1', 'town1', 'city2', 'town2', 'name2']

To do that I was inspired by this post :

Python: Rename duplicates in list with progressive numbers without sorting list

And I wrote this code that doesn't ! Can anyone find what it misses or tell me if it is completly wrong !

def unique(mylist):
newlist = []

for i, v in enumerate(mylist):
    totalcount = mylist.count(v)
    print('Step 1, totalcount : '+ str(totalcount))
    count = mylist[:i].count(v)
    print('Step 2, count : '+ str(count))

    if totalcount > 1 :
        print('Logic test :' + str(bool((v + str(count + 1)) in mylist)))
        while (v + str(count + 1)) in mylist :
            count += 1 
        list[i] = v + str(count+1)
        
    else : 
        list[i] = v

    newlist.append(list[i])
    print('Step 3 : '+str(newlist))

return newlist

The result of my code is :

['name', 'city1', 'city', 'name1', 'town1', 'city2', 'town', 'name2']

Because I can find the code, I am thinking to apply a function that removes only the last digits of a string like the following function and then indexing with one of the codes present in the link. However, I would find more elegant to do it directly. What do you think ?

def delete_digit_end(string):

name_parts=re.findall(r'[^\d_]+|[^\D]+|[^\W_]+|[\W_]+', string) # this part creates a list by splitting the digits,
# letters and '-_'

lenght=len(name_parts)-1 #we want to analize the last element of the list, if it contains digits or '_-'

# We do a loop while to test if the parts have digits or '-_', if true we execute the loop until it is false 
while name_parts[lenght].isdigit() : 
    # if it is true it will remove them
    name_parts[lenght]=''# it will remove them
    lenght -= 1 # if the condition was true, we continue with one inferior part

new_string = ''.join(map(str,name_parts))# now that we have cleaned if it was necessary we concatenate them

return new_string
Maikiii
  • 421
  • 4
  • 13
  • does the order really matters in expected output? – deadshot Oct 09 '20 at 18:53
  • That depends on what you mean; if it is the indexing not really, it is why I thought just to remove all the index existing to have the base. However I have two others lists, one for a path and another with the file extension, so I have to keep the position of the string on the list the same. The principal is to avoid the overwitten (error window when I move files and rename them. – Maikiii Oct 09 '20 at 19:05
  • I mean `['name1', 'name2', 'name3', 'city1', etc]` – deadshot Oct 09 '20 at 19:07
  • ahh no but it can be '['name1', 'city1', 'city2', name2,...] so why I thought removing al the digits at the end of the inputs. – Maikiii Oct 09 '20 at 19:12
  • It misses correct indentation. – mkrieger1 Oct 09 '20 at 19:29

1 Answers1

1

Here is something you can work on :

l = ['name', 'city', 'city', 'name1', 'town', 'city2', 'town', 'name2']

new_list = []

for item in l:
    i = 1
    if item[-1].isnumeric():
        new_list.append(item)
    else:
        while item + str(i) in l or item + str(i) in new_list:
            i += 1
        new_list.append(item + str(i))

print(new_list)

Output :

['name3', 'city1', 'city3', 'name1', 'town1', 'city2', 'town2', 'name2']
Amiral ASTERO
  • 365
  • 1
  • 6