[英]Sort nested list based on pattern in another list
I have following problem:我有以下问题:
List to be sorted:要排序的列表:
[['[check116] Ensure ...', 'azure-ad-role-manager ...'],
['[check28] Ensure ...', 'eu-west-1: Key ...', 'eu-central-1: Key ...'],
['[check41] Ensure ...', 'Found ...']]
Pattern:图案:
["[check28] Ensure ...",
"[check116] Ensure ...",
"[check41] Ensure ..."]
Desired output:期望的输出:
[['[check28] Ensure ...', 'eu-west-1: Key ...', 'eu-central-1: Key ...'],
['[check116] Ensure ...', 'azure-ad-role-manager ...'],
['[check41] Ensure ...', 'Found ...']]
I've tried: Sorting list based on values from another list and few other solutions - but they mainly base on sorting the pattern's int values, and it's not the case in my problem.我试过: 根据另一个列表中的值和其他一些解决方案对列表进行排序- 但它们主要基于对模式的 int 值进行排序,而在我的问题中并非如此。
Thanks in advance for any hints or solutions.提前感谢您提供任何提示或解决方案。
You can use list.index
in your key=
function:您可以在
key=
函数中使用list.index
:
lst = [
["[check116] Ensure ...", "azure-ad-role-manager ..."],
["[check28] Ensure ...", "eu-west-1: Key ...", "eu-central-1: Key ..."],
["[check41] Ensure ...", "Found ..."],
]
pattern = [
"[check28] Ensure ...",
"[check116] Ensure ...",
"[check41] Ensure ...",
]
out = sorted(lst, key=lambda k: pattern.index(k[0]))
print(out)
Prints:印刷:
[
["[check28] Ensure ...", "eu-west-1: Key ...", "eu-central-1: Key ..."],
["[check116] Ensure ...", "azure-ad-role-manager ..."],
["[check41] Ensure ...", "Found ..."],
]
Probably you can try this,或许你可以试试这个
input_ = [['[check116] Ensure ...', 'azure-ad-role-manager ...'],
['[check28] Ensure ...', 'eu-west-1: Key ...', 'eu-central-1: Key ...'],
['[check41] Ensure ...', 'Found ...']]
sorted(input_, key=lambda x: len(x), reverse=True)
[['[check28] Ensure ...', 'eu-west-1: Key ...', 'eu-central-1: Key ...'],
['[check116] Ensure ...', 'azure-ad-role-manager ...'],
['[check41] Ensure ...', 'Found ...']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.