python - get the indexes of the N largest values in a list such that these values respect a given condition -


ids = [ i, v in heapq.nlargest(rn, enumerate(score_real_test), key=operator.itemgetter(1))]  

this give indexes of rn largest values in list score_real_test. instead possible indexes of rn largest values "are in list score_real_test , satisfy boolean condition cond" ?

ids should contains rn indexes.

edit: use solution not best 1 :

score_real_test_2 = np.sort( [ v i,v in enumerate(score_real_test) if pred_real_test[i] == novel ] ) score_real_test_2 = score_real_test_2[len(score_real_test_2)-rn:] large_dist_ids = [i in range(len(score_real_test)) if score_real_test[i] in score_real_test_2] 

if want keep one-liner, can replace enumerate list comprehension(or generator expression) pre-filters data based on condition.

like so:

ids = [i i, v in heapq.nlargest(rn, [(j,v) in enumerate(score_real_test) if pred_real_test[i] == novel], key=operator.itemgetter(1))]  

or if want keep clearer, can add filtering step before selection(still no need pre-sorting):

tmp = [item item in enumerate(score_real_test) if pred_real_test[item[0]] == novel] ids = [i i, v in heapq.nlargest(rn, tmp, key=operator.itemgetter(1))]  

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -