用 glob 迭代特定目录会给我相同的有序输出——这可能吗?

Iterating specific directories with glob gives me same ordered outputs - is it possible?

我想问一些关于glob函数的问题。我使用它以特定顺序遍历输入中给出的目录(请参阅列表“codes=[...]”)。我知道 glob 不提供排序的输出,但是当我根据特定输入对其进行迭代时,它似乎按照我提交的顺序工作。 为了更好地理解,这是我的代码:

import glob
import yaml

codes=['a1','b1','c1',
       'a2','b2','c2']

#what I want to get from the files
resolutions = []

# get directories
for code in codes:
    directories = glob.glob("../../DATA/{}".format(code))
   
# browse directories, in the directory "a1" is yaml file with name "a1.yaml" and this is what I want to open
    for directory in directories_sorted:
        pdb_code = directory.split("/")[-1]
        yaml_name = directory + "/" + pdb_code + ".yaml"
    
        with open(yaml_name) as stream:
            yaml_content = yaml.load(stream, Loader=yaml.FullLoader)
            resolution = yaml_content["Resolution"]
            resolutions.append(resolution)

print(resolutions)

我的输出如下所示:

[3.9, 3.9, 3.6, 3.6, 3.64, 3.32]

当我检查 yaml 文件时,确实是 a1 有 3.9,b1 有 3.9,等等,最后输入的 c2 确实有 3.32。因此,输出似乎与我在“代码”中的输入顺序相同。可能吗?我只是想确定我的结果没问题。

您将有序输入与输出混淆了。是的,您多次以非常特定的顺序 运行ning glob。每个 glob 实例 运行 returns 每个 运行 的内容按照您提交的顺序排列,但搜索的每个文件夹中的内容未排序,至少不是按 Python。 glob returns 文件的每个实例都按照文件系统 returns 它们的顺序排列,无论是否恰好是这样排列的。