問題描述
給定這樣的列表:
mylist = ["name", "state", "name", "city", "name", "zip", "zip"]
我想通過附加一個數字來重命名重復項以獲得以下結果:
I would like to rename the duplicates by appending a number to get the following result:
mylist = ["name1", "state", "name2", "city", "name3", "zip1", "zip2"]
我不想更改原始列表的順序.針對此相關 Stack Overflow 問題建議的解決方案對列表進行排序,我不想這樣做.
I do not want to change the order of the original list. The solutions suggested for this related Stack Overflow question sorts the list, which I do not want to do.
推薦答案
我會這樣做.我把它寫成一個更通用的實用函數,因為人們似乎喜歡這個答案.
This is how I would do it. I wrote this into a more generalized utility function since people seem to like this answer.
mylist = ["name", "state", "name", "city", "name", "zip", "zip"]
check = ["name1", "state", "name2", "city", "name3", "zip1", "zip2"]
copy = mylist[:] # so we will only mutate the copy in case of failure
from collections import Counter # Counter counts the number of occurrences of each item
from itertools import tee, count
def uniquify(seq, suffs = count(1)):
"""Make all the items unique by adding a suffix (1, 2, etc).
`seq` is mutable sequence of strings.
`suffs` is an optional alternative suffix iterable.
"""
not_unique = [k for k,v in Counter(seq).items() if v>1] # so we have: ['name', 'zip']
# suffix generator dict - e.g., {'name': <my_gen>, 'zip': <my_gen>}
suff_gens = dict(zip(not_unique, tee(suffs, len(not_unique))))
for idx,s in enumerate(seq):
try:
suffix = str(next(suff_gens[s]))
except KeyError:
# s was unique
continue
else:
seq[idx] += suffix
uniquify(copy)
assert copy==check # raise an error if we failed
mylist = copy # success
如果您想在每個計數前添加下劃線,您可以執行以下操作:
If you wanted to append an underscore before each count, you could do something like this:
>>> mylist = ["name", "state", "name", "city", "name", "zip", "zip"]
>>> uniquify(mylist, (f'_{x!s}' for x in range(1, 100)))
>>> mylist
['name_1', 'state', 'name_2', 'city', 'name_3', 'zip_1', 'zip_2']
...或者如果您想改用字母:
...or if you wanted to use letters instead:
>>> mylist = ["name", "state", "name", "city", "name", "zip", "zip"]
>>> import string
>>> uniquify(mylist, (f'_{x!s}' for x in string.ascii_lowercase))
>>> mylist
['name_a', 'state', 'name_b', 'city', 'name_c', 'zip_a', 'zip_b']
注意:這不是最快的算法;為此,請參閱 ronakg 的答案.上述函數的優點是易于理解和閱讀,除非您有一個非常大的列表,否則您不會看到太大的性能差異.
NOTE: this is not the fastest possible algorithm; for that, refer to the answer by ronakg. The advantage of the function above is it is easy to understand and read, and you're not going to see much of a performance difference unless you have an extremely large list.
這是我在單行中的原始答案,但是順序沒有保留,它使用 .index
方法,這是非常次優的(如 DTing的答案).請參閱 queezz 的答案,了解保持秩序的良好雙線".
Here is my original answer in a one-liner, however the order is not preserved and it uses the .index
method, which is extremely suboptimal (as explained in the answer by DTing). See the answer by queezz for a nice 'two-liner' that preserves order.
[s + str(suffix) if num>1 else s for s,num in Counter(mylist).items() for suffix in range(1, num+1)]
# Produces: ['zip1', 'zip2', 'city', 'state', 'name1', 'name2', 'name3']
這篇關于Python:用漸進式數字重命名列表中的重復項而不對列表進行排序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!