有没有用Python快速标记列表的方法?

有没有用Python快速标记列表的方法?,python,list,dictionary,Python,List,Dictionary,我有一个20万元素的列表。这些元素是7个不同的标签(实际上是一个水果列表)。我需要给每个水果分配一个编号 有没有快速的方法可以做到这一点 到目前为止我已经写了这篇文章。。而且这需要很长时间 dic,i = {},0.0 for idx,el in enumerate(listFruit): if dic.has_key(el) is not True: dic[el] = i i+=1.0 listFruit[idx] = dic[el] 使用装

我有一个20万元素的列表。这些元素是7个不同的标签(实际上是一个水果列表)。我需要给每个水果分配一个编号

有没有快速的方法可以做到这一点

到目前为止我已经写了这篇文章。。而且这需要很长时间

dic,i = {},0.0
for idx,el in enumerate(listFruit):
    if dic.has_key(el) is not True:
        dic[el] = i
        i+=1.0
    listFruit[idx] = dic[el]
使用装配好的a作为工厂生产下一个价值;这将避免您自己测试每个键以及手动递增

然后使用列表理解将这些数字放入列表:

from collections import defaultdict
from functools import partial
from itertools import count

unique_count = defaultdict(partial(next, count(1)))
listFruit[:] = [unique_count[el] for el in listFruit]
在周围创建一个包装器,以确保代码在Python2或Python3中工作

我在这里使用了一个整数计数,从
1
开始。如果坚持使用浮点值,则可以用
count(1.0)
替换
count(1)
;您将获得
1.0
2.0
3.0
,等等

演示:

>>从集合导入defaultdict
>>>从functools导入部分
>>>从itertools导入计数
>>>从随机进口选择
>>>水果=[“苹果”、“香蕉”、“梨”、“樱桃”、“瓜”、“猕猴桃”、“菠萝”]
>>>listFruit=[选择(水果)uuxRange(100)]
>>>unique_count=defaultdict(部分(下一步,计数(1)))
>>>[listFruit中el的唯一计数[el]
[1, 2, 3, 2, 4, 5, 6, 7, 1, 2, 4, 6, 3, 7, 3, 4, 5, 2, 5, 7, 3, 5, 1, 3, 3, 5, 2, 2, 6, 4, 6, 2, 1, 1, 3, 6, 6, 4, 7, 2, 6, 4, 5, 2, 1, 7, 7, 7, 4, 3, 7, 3, 1, 1, 5, 3, 3, 6, 5, 6, 1, 4, 3, 7, 2, 7, 7, 4, 7, 1, 4, 3, 7, 3, 4, 5, 1, 5, 5, 1, 5, 6, 3, 4, 3, 1, 1, 1, 5, 7, 2, 2, 6, 3, 6, 1, 1, 6, 5, 4]
>>>唯一计数
defaultdict(,{'kiwi':4,'apple':1,'cherry':5,'pear':2,'菠萝':6,'Melan':7,'banana':3})
像这样的

结果:
[('apple',2),('banana',3),('草莓',0),('西瓜',1),('apple',2),('西瓜',1)]

或者
result=[如果水果中有x,则水果列表中的x得到(x)]


结果-
[2,3,0,1,2,1]

您的意思是要为每个水果分配一个唯一的编号?是的。苹果应该永远是1,香蕉2,草莓3。。。。直到ananas 7。注意:
has_key
已被弃用。要检查某个键是否在字典中,请使用
key in dictionary
。在您的情况下:
如果el不在dic中
。我也不知道为什么要分配浮点数……因为我想做一个平滑运算。哈哈。谢谢。@Dirty_Fox:你发布的代码建议你应该在
0.0
开始计数,而不是
1
,并且是你在列表中首先找到水果的顺序决定了计数(因此
Apple
只有在遇到第一个唯一的水果时才会是
0.0
部分(下一步,计数(1))
在python2和pytho3中都能工作。或者
lambda:next(count(1))
@Bakuriu:是的,谢谢。我在争论是否要让事情进一步复杂化(在认知负荷方面)。我更喜欢
partial();lambda:next(c)
否则它是一个常量function@Bakuriu:
lambda c=count(1):下一个(c)
也可以。:-)
>>> from collections import defaultdict
>>> from functools import partial
>>> from itertools import count
>>> from random import choice
>>> fruits = ['apple', 'banana', 'pear', 'cherry', 'melon', 'kiwi', 'pineapple']
>>> listFruit = [choice(fruits) for _ in xrange(100)]
>>> unique_count = defaultdict(partial(next, count(1)))
>>> [unique_count[el] for el in listFruit]
[1, 2, 3, 2, 4, 5, 6, 7, 1, 2, 4, 6, 3, 7, 3, 4, 5, 2, 5, 7, 3, 5, 1, 3, 3, 5, 2, 2, 6, 4, 6, 2, 1, 1, 3, 6, 6, 4, 7, 2, 6, 4, 5, 2, 1, 7, 7, 7, 4, 3, 7, 3, 1, 1, 5, 3, 3, 6, 5, 6, 1, 4, 3, 7, 2, 7, 7, 4, 7, 1, 4, 3, 7, 3, 4, 5, 1, 5, 5, 1, 5, 6, 3, 4, 3, 1, 1, 1, 5, 7, 2, 2, 6, 3, 6, 1, 1, 6, 5, 4]
>>> unique_count
defaultdict(<functools.partial object at 0x1026c5788>, {'kiwi': 4, 'apple': 1, 'cherry': 5, 'pear': 2, 'pineapple': 6, 'melon': 7, 'banana': 3})
fruit_list = ['apple', 'banana', 'strawberry', 'watermelon','apple','watermelon']

unique_fruits = [x for x in set(fruit_list)]
fruit_dict = dict((unique_fruits[y],y) for y in range(len(unique_fruits)))
result = [(x, fruit_dict.get(x)) for x in fruit_list if x in fruit_dict.keys()]