小编Yfi*_*iua的帖子

使用唯一索引索引列表

我有一个清单说l = [10,10,20,15,10,20].我想为每个唯一值分配一个特定的"索引"来获取[1,1,2,3,1,2].

这是我的代码:

a = list(set(l))
res = [a.index(x) for x in l]
Run Code Online (Sandbox Code Playgroud)

结果证明非常慢.

l拥有1M个元素和100K独特元素.我也尝试过使用lambda和排序的地图,这没有用.这样做的理想方法是什么?

python indexing list

27
推荐指数
4
解决办法
4058
查看次数

标签 统计

indexing ×1

list ×1

python ×1