擅长:python、mysql、java
<p>我只使用文件名而不是日期来解决它。在</p>
<p>在对整个文件名应用条件或字母数字排序之前,必须先转换为整数</p>
<p>概念证明:</p>
<pre><code>import re
list_of_files = ["file1","file100","file4","file7"]
def extract_number(f):
s = re.findall("\d+$",f)
return (int(s[0]) if s else -1,f)
print(max(list_of_files,key=extract_number))
</code></pre>
<p>结果:<code>file100</code></p>
<ul>
<li>key函数提取在文件末尾找到的数字并将其转换为整数,如果没有找到,则返回<code>-1</code></li>
<li>您不需要<code>sort</code>来找到最大值,只需将密钥直接传递给<code>max</code></li>
<li>如果两个文件具有相同的索引,请使用完整文件名来中断连接(这解释了<code>tuple</code>键)</li>
</ul>