Python 2.7: Dedup list by adding suffix

I am not sure that I am thinking correctly about this problem. I would like to write a function that takes a list with duplicates and adds an iterative suffix to β€œdeduplicate” the list.

For instance:

dup_list = ['apple','banana','cherry','banana','cherry','orange','cherry'] 

Return Direction:

 deduped = ['apple','banana1','cherry1','banana2','cherry2','orange','cherry3'] 

My instinct was to use the pop function, iterate through the list with a while statement, for example:

 def dedup_suffix(an_list): dedup=[] for each in an_list: an_list.pop(an_list.index(each)) #pop it out i=1 #iterator while each in an_list: an_list.pop(an_list.index(each)) i+=1 appendage=str(each)+"_"+str(i) else: appendage=str(each) dedup.append(appendage) return dedup 

But:

 >>> dedup_suffix(dup_list) 

['apple', 'cherry', 'orange']

Appreciate any pointers.

+4
source share
2 answers

You can use Counter to track the number of occurrences. I assume your example is true for apple , so you don't want to add zero to the first occurrence. To do this, you need a little logic:

 from collections import Counter counter = Counter() dup_list = ['apple','banana','cherry','banana','cherry','orange','cherry'] deduped = [] for name in dup_list: new = name + str(counter[name]) if counter[name] else name counter.update({name: 1}) deduped.append(new) 
+4
source

You can count the number of duplicates using the collection.Counter object. Then create a new list, iterate through

 dup_list = ['apple','banana','cherry','banana','cherry','orange','cherry'] c = Counter(dup_list) dedup=[] for w in c: n = c[w] if n == 1: dedup.append(w) else: for i in range(1,n+1): dedup.append(w+str(i)) 
+1
source

Source: https://habr.com/ru/post/1487954/


All Articles