Python : How to find duplicates in a list and upda

2020-05-09 22:00发布

I have a list of items like this:

['T1','T2','T2','T2','T2','T3','T3' ]

I need to make sure that duplicates are renamed with a progressive letter added like this:

['T1','T2A','T2B','T2C','T2D','T3A','T3B']

but only if there is more than 1 occurrence of the same item.

Also, is it possible to do so without generating a new list?

Any ideas?

3条回答
再贱就再见
2楼-- · 2020-05-09 22:39
def fix(L):
    d = {}
    for i in xrange(len(L)):
        d[L[i]] = d.get(L[i],0)+1
        if d[L[i]] > 1:
            if d[L[i]] == 2: L[L.index(L[i])] += 'A'
            L[i] += chr(64+d[L[i]])
查看更多
萌系小妹纸
3楼-- · 2020-05-09 22:58

Considering the list is sorted, this will modify the list in-place. If the list is not sorted then you can sort it first using lis.sort():

>>> from string import ascii_uppercase
>>> from itertools import groupby
>>> from collections import Counter
>>> lis = ['T1', 'T2', 'T2', 'T2', 'T2', 'T3', 'T3']
>>> c = Counter(lis)
>>> for k, v in groupby(enumerate(lis), key = lambda x:x[1]):
    l = list(v)
    if c[k] > 1:
        for x, y in zip(l, ascii_uppercase):
            lis[x[0]] = x[1] + y
...             
>>> lis
['T1', 'T2A', 'T2B', 'T2C', 'T2D', 'T3A', 'T3B']
查看更多
女痞
4楼-- · 2020-05-09 23:02
from collections import Counter
from string import ascii_uppercase as letters

def gen(L):
    c = Counter(L)
    for elt, count in c.items():
        if count == 1:
            yield elt
        else:
            for letter in letters[:count]:
                yield elt + letter

Now:

>>> L = ['T1','T2','T2','T2','T2','T3','T3']
>>> list(gen(L))
['T2A', 'T2B', 'T2C', 'T2D', 'T3A', 'T3B', 'T1']
查看更多
登录 后发表回答