Issue
I am trying to get the indices of the values in the array from maximum to minimum without sorting the indices positions. Here is the current code I have
s = [20, 30, 45, 14, 59]
ans = sorted(range(len(s)), key=lambda k: s[k], reverse=True)
print ans
but I am getting the output as [4, 2, 1, 0, 3]
which is based on sorting. I don't want the sorted list of indexes instead I want them in same position. Output should be [3, 2, 1, 4, 0]
. Is there any way to achieve this?
For 2D: Now if there is another array (only with -1, 0, 1) associated with the first array sv = [(1,0), (-1,1), (-1,0), (0,-1), (1,1)]
. Here each value in sv is indexed and bind to same output array [3, 2, 1, 4, 0]
. Now based on certain condition say
for i in s if any val[i] < max(val[i])*25/100
value will be removed from s as well as sv and also its indexing. So in above case new s, new sv and new indexing output will be
s = [20, 30, 45, 59]
indexing = [3, 2, 1, 0]
sv = [(1,0), (-1,1), (-1,0), (1,1)]
Solution
You can use a dict that maps each list item to its sorted index:
mapping = {k: i for i, k in enumerate(sorted(s, reverse=True))}
print([mapping[i] for i in s])
This outputs:
[3, 2, 1, 4, 0]
Answered By - blhsing
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.