gpt4 book ai didi

python - Sorted(items, lambda) 多个项目,一个颠倒

转载 作者:行者123 更新时间:2023-11-28 22:43:38 25 4
gpt4 key购买 nike

我将如何进行以下排序?

import re
list_of_strings=['hulu_delta_20150528.xml', 'hulu_delta_20150524',
'playstation_full_20150529', 'hulu_full_20150528.xml']
sorted(list_of_strings, key=lambda x: (
x[:3],
re.search(r'\d{8}',x).group() if re.search(r'\d{8}',x) else None,
-x # How would this be done as a third criteria?
))

特别是,我如何按照反向字母顺序作为第三个标准对项目进行排序?最终结果应该是:

['hulu_delta_20150524', 'hulu_full_20150528.xml', 'hulu_delta_20150528.xml', 'playstation_full_20150529']

最佳答案

您可以比较项目的负序数值以按字母顺序反向比较它们:

#  All hulu strings have same date 
>>> list_of_strings=['hulu_delta_20150528.xml', 'hulu_delta_20150524',
'playstation_full_20150529', 'hulu_full_20150528.xml']
>>> files = sorted(list_of_strings, key=lambda x: (
x[:3],
re.search(r'\d{8}',x).group() if re.search(r'\d{8}', x) else None,
[-ord(c) for c in x]
))
>>> files
['hulu_delta_20150524', 'hulu_full_20150528.xml', 'hulu_delta_20150528.xml', 'playstation_full_20150529']

关于python - Sorted(items, lambda) 多个项目,一个颠倒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30541802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com