简体   繁体   English

Python - 通过添加计数使列表中的非唯一项目唯一

[英]Python - make non-unique items in list unique by adding count

How would I make the items in my list unique by concatenating a count, starting from 1 for each unique value?如何通过连接计数使列表中的项目变得唯一,每个唯一值从 1 开始?

so, for example:所以,例如:

sheep, sheep, tiger, sheep, hippo, tiger

becomes:变成:

sheep1, sheep2, tiger1, sheep3, hippo1, tiger2

Here's how you could use Counter to do it.下面是如何使用 Counter 来做到这一点。

from collections import Counter

s = ["sheep", "sheep", "tiger", "sheep", "hippo", "tiger"]
u = [ f"{a}{c[a]}" for c in [Counter()] for a in s if [c.update([a])] ]

print(u)

['sheep1', 'sheep2', 'tiger1', 'sheep3', 'hippo1', 'tiger2']

note that, if your strings can have a numeric suffix, this would not be sufficient to cover all cases (eg ['alpha']*11+['alpha1'] would repeat 'alpha11' )请注意,如果您的字符串可以有数字后缀,则这不足以涵盖所有情况(例如['alpha']*11+['alpha1']会重复'alpha11'

you can use a simple for loop:你可以使用一个简单的for循环:

l = ['sheep', 'sheep', 'tiger', 'sheep', 'hippo', 'tiger']

count = {}
output = []
for s in l:
    if s in count:
        count[s] += 1
    else:
        count[s] = 1

    output.append(f'{s}{count[s]}')

output

output:输出:

['sheep1', 'sheep2', 'tiger1', 'sheep3', 'hippo1', 'tiger2']

Using a combination of defaultdict and count :使用defaultdictcount的组合:

>>> from collections import defaultdict
>>> from itertools import count
>>> s = ["sheep", "sheep", "tiger", "sheep", "hippo", "tiger"]
>>> d = defaultdict(lambda: count(1))
>>> [f'{x}{next(d[x])}' for x in s]
['sheep1', 'sheep2', 'tiger1', 'sheep3', 'hippo1', 'tiger2']

count is an object that yields ever-increasing numbers as you iterate over it; count是一个对象,当您对其进行迭代时,它会产生不断增加的数字; calling next gives you the next number in the sequence.调用next为您提供序列中的下一个数字。

The defaultdict creates a new count instance every time you try to access a new key, while saving the newly created instance for the next time you see the same key.每次尝试访问新密钥时, defaultdict都会创建一个新的count实例,同时保存新创建的实例以供下次看到相同的密钥时使用。

I had a very similar need where the output would be:我有一个非常相似的需求,输出将是:

['sheep', 'sheep1', 'tiger', 'sheep2', 'hippo', 'tiger1']

I approached it a little differently looking for an O(n) solution and extended the dictionary class.我以不同的方式寻找 O(n) 解决方案并扩展了字典类。

class IncDict(dict):
    def __missing__(self,key):
        return -1

    def __getitem__(self,key):
        val = dict.__getitem__(self,key)
        val+=1
        dict.__setitem__(self,key,val)
        if val==0:
            return key
        else:
            return key+str(val)

l = ['sheep', 'sheep', 'tiger', 'sheep', 'hippo', 'tiger']
uniquify = IncDict()
[uniquify[x] for x in l]

Output:输出:

['sheep', 'sheep1', 'tiger', 'sheep2', 'hippo', 'tiger1']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM