来自 os.listdir() 的非字母数字列表顺序 [英] Non-alphanumeric list order from os.listdir()

查看:28
本文介绍了来自 os.listdir() 的非字母数字列表顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用python来处理数据目录.最近,我注意到列表的默认顺序已更改为几乎无意义的东西.例如,如果我在包含以下子目录的当前目录中:run01、run02、...run19、run20,然后我从以下命令生成一个列表:

I often use python to process directories of data. Recently, I have noticed that the default order of the lists has changed to something almost nonsensical. For example, if I am in a current directory containing the following subdirectories: run01, run02, ... run19, run20, and then I generate a list from the following command:

dir = os.listdir(os.getcwd())

然后我通常会按这个顺序得到一个列表:

then I usually get a list in this order:

dir = ['run01', 'run18', 'run14', 'run13', 'run12', 'run11', 'run08', ... ]

等等.订单曾经是字母数字.但是这个新秩序已经存在了一段时间.

and so on. The order used to be alphanumeric. But this new order has remained with me for a while now.

是什么决定了这些列表的(显示)顺序?

What is determining the (displayed) order of these lists?

推荐答案

我认为顺序与文件在 FileSystem 上的索引方式有关.如果你真的想让它遵守某个顺序,你可以在获取文件后对列表进行排序.

I think the order has to do with the way the files are indexed on your FileSystem. If you really want to make it adhere to some order you can always sort the list after getting the files.

这篇关于来自 os.listdir() 的非字母数字列表顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆