How to arrange items in ascending order in a Python list

Question:

I have the below files in a directory:

enter image description here

Using os.listdir() I am reading all the files and then saving them in a list. 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. 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. 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. How can I correct this issue?

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"]
Asked By: S Andrew

||

Answers:

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]))
Answered By: Avinash

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.

Answered By: GCMeccariello

glob is a better choice for isolating only the files you’re really interested in.

Build your list of files.

Sort the list based on the 4th token in each filename – i.e., the number between ‘Epoch’ and ‘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.

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]
Answered By: Fred
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.