简体   繁体   English

按从最大值到最小值的值顺序获取索引,而无需对输出索引列表进行排序,也可以修改另一个列表

[英]Get the index in order of values from max to min without sorting the output index list and amend another list as well

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. 但是我得到的输出是[4, 2, 1, 0, 3] ,它是基于排序的。 I don't want the sorted list of indexes instead I want them in same position. 我不需要索引的排序列表,而是希望它们位于相同的位置。 Output should be [3, 2, 1, 4, 0] . 输出应为[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)] . 对于2D:现在,如果存在与第一个数组相关联的另一个数组(仅包含-1、0、1),则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] . 在这里,sv中的每个值都被索引并绑定到相同的输出数组[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. 值将从s和sv及其索引中删除。 So in above case new s, new sv and new indexing output will be 因此,在上述情况下,new,new sv和new indexing输出将是

s = [20, 30, 45, 59]
indexing = [3, 2, 1, 0]
sv = [(1,0), (-1,1), (-1,0), (1,1)]

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]
s = [20, 30, 45, 14, 59]
ss = sorted(s,reverse=True)


print([s.index(i) for i in ss]) ## gives [4, 2, 1, 0, 3]
print([ss.index(i) for i in s]) ## gives [3, 2, 1, 4, 0]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取列表中最大值的索引,然后使用最大值的索引从另一个列表中打印值? - How can I get the index of max values in list and then print the values from another list with max's index? 通过嵌套列表中的元素索引查找嵌套列表的最小值和最大值 - find the min and max values of nested list by index of element in nested list 在嵌套列表中获取一个索引为max或min的结果列表 - Get one resulting list with the max or min by index in a nested list 获取列表中具有相同最大值的最后一个索引 - Get the last index with same max values in a list 在不使用 min、max 和 sort 等内置函数对列表进行排序时,使用 while 1 而不是 while timelist2 时出现索引错误 - when sorting list without using built in functions like min,max and sort, index error when using while 1 instead of while timelist2 使用列表中的 max()/min() 获取返回的最大或最小项的索引 - Getting the index of the returned max or min item using max()/min() on a list 按列表中的索引查找最小值/最大值 - Find min/max by index in list of lists 如何在不使用迭代的情况下从另一个 df 获取具有特定索引、列的值列表? - How can I get list of values with specific index, column from another df without using iteration? 从python中的另一个列表获取列表的索引 - Get the index of a list from another list in python 找到第二个索引的最小值,然后找到列表Python的第三个索引的最大值 - find the min of the second index, then find the max of the third index of a list Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM