[英]How to append count numbers to duplicates in a list in Python?
Here is a list containing duplicates:这是一个包含重复项的列表:
l1 = ['a', 'b', 'c', 'a', 'a', 'b']
Here is the desired result:这是想要的结果:
l1 = ['a', 'b', 'c', 'a_1', 'a_2', 'b_1']
How can the duplicates be renamed by appending a count number?如何通过附加计数来重命名重复项?
Here is an attempt to achieve this goal;这是实现这一目标的尝试; however, is there a more Pythonic way?
然而,有没有更 Pythonic 的方式?
for index in range(len(l1)):
counter = 1
list_of_duplicates_for_item = [dup_index for dup_index, item in enumerate(l1) if item == l1[index] and l1.count(l1[index]) > 1]
for dup_index in list_of_duplicates_for_item[1:]:
l1[dup_index] = l1[dup_index] + '_' + str(counter)
counter = counter + 1
In Python, generating a new list is usually much easier than changing an existing list.在 Python 中,生成新列表通常比更改现有列表容易得多。 We have generators to do this efficiently.
我们有生成器可以有效地做到这一点。 A dict can keep count of occurrences.
dict 可以记录出现次数。
l = ['a', 'b', 'c', 'a', 'a', 'b']
def rename_duplicates( old ):
seen = {}
for x in old:
if x in seen:
seen[x] += 1
yield "%s_%d" % (x, seen[x])
else:
seen[x] = 0
yield x
print list(rename_duplicates(l))
I would do something like this:我会做这样的事情:
a1 = ['a', 'b', 'c', 'a', 'a', 'b']
a2 = []
d = {}
for i in a1:
d.setdefault(i, -1)
d[i] += 1
if d[i] >= 1:
a2.append('%s_%d' % (i, d[i]))
else:
a2.append(i)
print a2
Based on your comment to @mathmike, if your ultimate goal is to create a dictionary from a list with duplicate keys, I would use a defaultdict
from the `collections Lib.根据您对@mathmike 的评论,如果您的最终目标是从具有重复键的列表中创建字典,我将使用`collections Lib 中的
defaultdict
。
>>> from collections import defaultdict
>>> multidict = defaultdict(list)
>>> multidict['a'].append(1)
>>> multidict['b'].append(2)
>>> multidict['a'].append(11)
>>> multidict
defaultdict(<type 'list'>, {'a': [1, 11], 'b': [2]})
I think the output you're asking for is messy itself, and so there is no clean way of creating it.我认为您要求的输出本身很混乱,因此没有干净的创建方法。
How do you intend to use this new list?您打算如何使用这个新列表? Would a dictionary of counts like the following work instead?
像下面这样的计数字典会起作用吗?
{'a':3, 'b':2, 'c':1}
If so, I would recommend:如果是这样,我会建议:
from collections import defaultdict
d = defaultdict(int) # values default to 0
for key in l1:
d[key] += 1
I wrote this approach for renaming duplicates in a list with any separator and a numeric or alphabetical postfix (eg _1, _2 or _a, _b, _c etc.).我编写了这种方法来重命名列表中的重复项,其中包含任何分隔符和数字或字母后缀(例如 _1、_2 或 _a、_b、_c 等)。 Might not be the best you could write efficient-wise, but I like this as a clean readable code which is also scalable easily.
可能不是你能编写的最好的高效明智的代码,但我喜欢这是一个干净可读的代码,它也很容易扩展。
def rename_duplicates(label_list, seperator="_", mode="numeric"):
"""
options for 'mode': numeric, alphabet
"""
import string
if not isinstance(label_list, list) or not isinstance(seperator, str):
raise TypeError("lable_list and separator must of type list and str, respectively")
for item in label_list:
l_count = label_list.count(item)
if l_count > 1:
if mode == "alphabet":
postfix_str = string.ascii_lowercase
if len(postfix_str) < l_count:
# do something
pass
elif mode == "numeric":
postfix_str = "".join([str(i+1) for i in range(l_count)])
else:
raise ValueError("the 'mode' could be either 'numeric' or 'alphabet'")
postfix_iter = iter(postfix_str)
for i in range(l_count):
item_index = label_list.index(item)
label_list[item_index] += seperator + next(postfix_iter)
return label_list
label_list = ['a', 'b', 'c', 'a', 'a', 'b']
use the function: rename_duplicates(label_list)
使用函数:
rename_duplicates(label_list)
result: ['a_1', 'b_1', 'c', 'a_2', 'a_3', 'b_2']
结果:
['a_1', 'b_1', 'c', 'a_2', 'a_3', 'b_2']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.