alex_bn_lee

导航

【381】python 获取列表中重复元素的索引值

参考:获取python的list中含有重复值的index方法_python_脚本之家  

核心思想:建立字典,遍历列表,把列表中每个元素和其索引添加到字典里面

cc = [1, 2, 3, 2, 4]
from collections import defaultdict
dd = defaultdict(list)
for k, va in [(v,i) for i, v in enumerate(cc)]:
    dd[k].append(va)
print(dd)

output:
defaultdict(<class 'list'>, {1: [0], 2: [1, 3], 3: [2], 4: [4]})

  

posted on 2019-03-19 07:50  McDelfino  阅读(7387)  评论(0编辑  收藏  举报