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

查看:110
本文介绍了来自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天全站免登陆