Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将唯一计数附加到列表中字符串的末尾?_Python_Python 3.x_List - Fatal编程技术网

Python 如何将唯一计数附加到列表中字符串的末尾?

Python 如何将唯一计数附加到列表中字符串的末尾?,python,python-3.x,list,Python,Python 3.x,List,我正在寻找类似的东西,但是Python 我有一个重复元素的列表: ["Apple", "Orange", "Apple", "Pear", "Banana", "Apple", "Apple", "Orange"] 我正在寻找一个列表表达式或方法,它将为我提供: ["Apple 1", "Orange 1", "Apple 2", "Pear 1", "Banana 1", "Apple 3", "Apple 4", "Orange 2"] 显然,保持秩序非常重要。选项1 collectio

我正在寻找类似的东西,但是Python

我有一个重复元素的列表:

["Apple", "Orange", "Apple", "Pear", "Banana", "Apple", "Apple", "Orange"]
我正在寻找一个列表表达式或方法,它将为我提供:

["Apple 1", "Orange 1", "Apple 2", "Pear 1", "Banana 1", "Apple 3", "Apple 4", "Orange 2"]

显然,保持秩序非常重要。

选项1
collections.Counter


选项2
itertools.count

,这是一个很好的选择

from itertools import count

mapping = {k : count(1) for k in set(lst)}   
lst2 = ['{} {}'.format(x, next(mapping[x])) for x in lst]

print(lst2)
['Apple 1', 'Orange 1', 'Apple 2', 'Pear 1', 'Banana 1', 'Apple 3', 'Apple 4', 'Orange 2']

这与上面的区别在于
映射的定义方式。

选项1
collections.Counter


选项2
itertools.count

,这是一个很好的选择

from itertools import count

mapping = {k : count(1) for k in set(lst)}   
lst2 = ['{} {}'.format(x, next(mapping[x])) for x in lst]

print(lst2)
['Apple 1', 'Orange 1', 'Apple 2', 'Pear 1', 'Banana 1', 'Apple 3', 'Apple 4', 'Orange 2']

这与上面的区别在于
映射的定义方式。

直截了当的方式似乎很明显:

>>> from collections import Counter
>>> counts = Counter()
>>> x = ["Apple", "Orange", "Apple", "Pear", "Banana", "Apple", "Apple", "Orange"]
>>> new_x = []
>>> for item in x:
...     counts[item] += 1
...     new_x.append(f"{item}{counts[item]}")
...
>>> new_x
['Apple1', 'Orange1', 'Apple2', 'Pear1', 'Banana1', 'Apple3', 'Apple4', 'Orange2']
>>>

直截了当的方法似乎很明显:

>>> from collections import Counter
>>> counts = Counter()
>>> x = ["Apple", "Orange", "Apple", "Pear", "Banana", "Apple", "Apple", "Orange"]
>>> new_x = []
>>> for item in x:
...     counts[item] += 1
...     new_x.append(f"{item}{counts[item]}")
...
>>> new_x
['Apple1', 'Orange1', 'Apple2', 'Pear1', 'Banana1', 'Apple3', 'Apple4', 'Orange2']
>>>
现在
数据
等于
[‘苹果1’、‘橙色1’、‘苹果2’、‘梨1’、‘香蕉1’、‘苹果3’、‘苹果4’、‘橙色2’]

这将修改给定的列表

现在
数据
等于
[‘苹果1’、‘橙色1’、‘苹果2’、‘梨1’、‘香蕉1’、‘苹果3’、‘苹果4’、‘橙色2’]


这将修改给定的列表。

您可以使用
itertools.count
对象,并避免使用
计数器
,因此
映射
将只是
{k:count()for e in x}
@juanpa.arrivillaga工作得太棒了!如果你想把它添加到你的答案中,让我知道,我会把它从我的答案中删除。我想知道哪一个会更快,你做过一些基准测试吗?@norok2不,我还没有机会做,但我希望它们大致相同……你可以使用
itertools.count
对象,并避免使用
计数器
,因此,
映射
将是
{k:count(),用于x}
@juanpa.arrivillaga工作得非常好!如果你想把它添加到你的答案中,让我知道,我会把它从我的答案中删除。我想知道哪一个会更快,你做过一些基准测试吗?@norok2不,我还没有机会做,但我希望它们大致相同……读者注意:
f
strings在python3.6(?)及以上版本上工作。对于较旧版本,请使用
str.format
。读者注意:
f
字符串适用于python3.6(?)及以上版本。对于旧版本,请使用
str.format