- 有一个含有相同字符串的数据,返回具有相同字符串的index
from collections import defaultdict
s = ['000000', '000000', '000000', '000001', '000002', '000001', '000003', '000003', '000002']
d = defaultdict(list)
for index, val in enumerate(s):
d[val].append(index)
>>> {'000000': [0, 1, 2], '000001': [3, 5], '000002': [4, 8], '000003': [6, 7]})
- 有一个List,排序后返回相应index
import numpy as np
l = [1,4,5,2,3]
l = np.array(l)
idx_asc = np.argsort(l) # ascending
idx_des = np.argsort(-l) # descending