我有以下问题:
要排序的列表:
[['[check116] Ensure ...', 'azure-ad-role-manager ...'],
['[check28] Ensure ...', 'eu-west-1: Key ...', 'eu-central-1: Key ...'],
['[check41] Ensure ...', 'Found ...']]
图案:
["[check28] Ensure ...",
"[check116] Ensure ...",
"[check41] Ensure ..."]
期望的输出:
[['[check28] Ensure ...', 'eu-west-1: Key ...', 'eu-central-1: Key ...'],
['[check116] Ensure ...', 'azure-ad-role-manager ...'],
['[check41] Ensure ...', 'Found ...']]
我试过:根据另一个列表中的值和其他一些解决方案对列表进行排序- 但它们主要基于对模式的 int 值进行排序,而在我的问题中并非如此。
提前感谢您提供任何提示或解决方案。
您可以list.index
在key=
函数中使用:
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)
印刷:
[
["[check28] Ensure ...", "eu-west-1: Key ...", "eu-central-1: Key ..."],
["[check116] Ensure ...", "azure-ad-role-manager ..."],
["[check41] Ensure ...", "Found ..."],
]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句