就地将排列应用于列表的方法? (按键排序的倒数) [英] In-place way to apply a permutation to a list? (inverse of sorting-by-key)
本文介绍了就地将排列应用于列表的方法? (按键排序的倒数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我想做的事
spam_list = ["We", "are", "the", "knights", "who", "say", "Ni"]
spam_order = [0,1,2,4,5,6,3]
spam_list.magical_sort(spam_order)
print(spam_list)
["We", "are", "the", "who", "say", "Ni", "knights"]
我可以使用enumerate
,list
等来执行此操作,但是我想直接影响spam_list
(例如list.sort()
),而不是像sorted()
I can do it with enumerate
, list
and so on, but I would like to directly affect spam_list
, like list.sort()
and not copy it like sorted()
编辑:推送了一个字符串示例,以避免索引和spam_list
Edit : pushed a string example to avoid confusion between indices and values of spam_list
编辑:原来是 Python的副本将并行数组排序到位?.好吧,我不能为SO一致性参数付出如此多的努力.
Edit : turned out this is a duplicate of Python sort parallel arrays in place?. Well, I can't delete so much efforts for SO consistency arguments.
推荐答案
您可以尝试:
spam_list = [spam_list[i] for i in spam_order]
这篇关于就地将排列应用于列表的方法? (按键排序的倒数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文