[英]How to arrange items in ascending order in list Python
I have the below files in a directory:我在一个目录中有以下文件:
Using os.listdir()
I am reading all the files and then saving them in a list.使用os.listdir()
我正在读取所有文件,然后将它们保存在列表中。 Below is the code:下面是代码:
y = []
files = os.listdir()
for file in files:
if "mb" in file:
file = file.split("-")
loss = file[5]
lossNum = loss.split('.pth')
y.append(round(float(lossNum[0]), 3))
print(y)
In above code, I am reading the file name and then splitting it so that I get the number for ex 8.199
or 6.184
and I am saving them in the list.在上面的代码中,我正在读取文件名,然后将其拆分,以便获得 ex 8.199
或6.184
的编号,并将它们保存在列表中。 Below is the output of the list:以下是列表的输出:
[8.2, 6.185, 4.115, 4.425, 3.897, 3.972, 5.672, 6.112, 6.129, 5.382, 4.558, 5.476, 4.526, 4.579]
Values in the above list is not as per the filenames.上面列表中的值与文件名不同。 For ex, value at index 0 and 1 are correct because in file name Epoch-0
and Epoch-1
has the same number but Epoch-2
has number 5.67
but index 2 of list contains 4.11
which is wrong.例如,索引 0 和 1 处的值是正确的,因为文件名Epoch-0
和Epoch-1
具有相同的编号,但Epoch-2
的编号为5.67
,但列表的索引 2 包含4.11
,这是错误的。 This is happening because when we do os.listdit()
it is automatically list Epoch-0, Epoch-1, and then Epoch-10, Epoch-11, Epoch-12 instead of Epoch-2, Epoch-3 and so on.这是因为当我们执行os.listdit()
时,它会自动列出 Epoch-0、Epoch-1,然后是 Epoch-10、Epoch-11、Epoch-12,而不是 Epoch-2、Epoch-3 等等。 How can I correct this issue.我该如何纠正这个问题。 Please help Thanks请帮忙谢谢
Files:文件:
["mb1-ssd-Epoch-0-Loss-8.199731510297386.pth",
"mb1-ssd-Epoch-1-Loss-6.184953727553376.pth",
"mb1-ssd-Epoch-10-Loss-4.114924973091193.pth",
"mb1-ssd-Epoch-11-Loss-4.4250144663110245.pth",
"mb1-ssd-Epoch-12-Loss-3.896865705473233.pth",
"mb1-ssd-Epoch-13-Loss-3.972265353245018.pth.filepart",
"mb1-ssd-Epoch-2-Loss-5.671893659946138.pth",
"mb1-ssd-Epoch-3-Loss-6.111974941945709.pth",
"mb1-ssd-Epoch-4-Loss-6.128832694703498.pth",
"mb1-ssd-Epoch-5-Loss-5.382261596949754.pth",
"mb1-ssd-Epoch-6-Loss-4.558234235881704.pth",
"mb1-ssd-Epoch-7-Loss-5.47572956253997.pth",
"mb1-ssd-Epoch-8-Loss-4.526285114541518.pth",
"mb1-ssd-Epoch-9-Loss-4.578502741535153.pth"]
Do this to sort your files in the order shown in image uploaded in your question:这样做可以按照问题中上传的图片中显示的顺序对文件进行排序:
files = os.listdir()
files.sort(key=lambda x: int(x.split("-")[3]))
You could split the data and save it accordingly the "Epoch-number".您可以拆分数据并将其相应地保存为“时代编号”。
An example:一个例子:
string = 'mb1-ssd-Epoch-5-Loss'
Number = string.split(sep='-')[3]
Output: 5
Take the name of the file.取文件名。 Apply the split function with the seperator and finally choose the right index.使用分隔符应用拆分功能,最后选择正确的索引。
glob is a better choice for isolating only the files you're really interested in. glob 是仅隔离您真正感兴趣的文件的更好选择。
Build your list of files.建立你的文件列表。
Sort the list based on the 4th token in each filename - ie, the number between 'Epoch' and 'Loss'根据每个文件名中的第 4 个标记对列表进行排序 - 即 'Epoch' 和 'Loss' 之间的数字
Split the filename on hyphen, take the last token and ignore the last 4 characters (.pth) and convert to rounded float and add to your list.在连字符上拆分文件名,取最后一个标记并忽略最后 4 个字符 (.pth) 并转换为四舍五入的浮点数并添加到您的列表中。
from glob import glob
y = []
for file in sorted(glob('mb*.pth'), key=lambda x: int(x.split('-')[3])):
n = file.split('-')[-1][:-4]
y.append(round(float(n), 3))
print(y)
For the list of files shown in the question, this results in:对于问题中显示的文件列表,这将导致:
[8.2, 6.185, 5.672, 6.112, 6.129, 5.382, 4.558, 5.476, 4.526, 4.579, 4.115, 4.425, 3.897]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.