Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有这个文件列表:
1.jpg, 2.jpg, ..., 10.jpg, ...
如果我执行 python os.listdir(".")我列出了所有文件,但按字母顺序排列,如下所示:
[1.jpg, 10.jpg, 11.jpg, ..., 2.jpg, 20.jpg....]
但我希望它们按其数值排序
[1.jpg, 2.jpg ,...10 ,11 ,12... ]
如何重新排序列表以完成此操作?
发送
In [51]: lis=["1.jpg","10.jpg","11.jpg","2.jpg","20.jpg"] In [52]: sorted(lis,key=lambda x:int(x.split(".")[0])) Out[52]: ['1.jpg', '2.jpg', '10.jpg', '11.jpg', '20.jpg']