2

I have a problem with sorting my strings which are stored in a list. The string is actually a path/filename of a measurement and looks e.g. like this:

'Data\Test1\Test1_<SomeParametersOfTheMeasurement>_-10_step1.txt'

  1. -10 stands for the temperature of the measurement. The temperature ranges from -20 to 80 °C (2 °C steps in between) -> 1st condtion I want to sort my strings from -20 to 80.

  2. step1 indicates the test number at each temperature. At each temperature I perform at least 20 test runs -> 2nd condition sort strings from 1 to 20.

My list should then look like this:

meas_files = [
'...._-20_step1.txt',
'...._-20_step2.txt',
'...._-20_step3.txt',
'...'
'...._-20_step20.txt',
'...._-18_step1.txt',
'...._-18_step2.txt',
'...._-18_step3.txt',
'...'
'...._-18_step20.txt',
'...._-16_step1.txt',
'...._-16_step2.txt',
'...._-16_step3.txt',
'...'
'...'
'...'
'...._80_step20.txt']

I have tried following: list(Tcl().call('lsort', '-dict', meas_files)) (one command I have used for sorting the list by step numbers) but this resulted in sorting the steps numbers right but it started with -2 to -20 and continued with 0 to 80 °C:

meas_files = [
'...._-2_step1.txt',
'...._-2_step2.txt',
'...._-2_step3.txt',
'...'
'...._-2_step20.txt',
'...._-4_step1.txt',
'...._-4_step2.txt',
'...._-4_step3.txt',
'...'
'...._-4_step20.txt',
'...._-6_step1.txt',
'...._-6_step2.txt',
'...._-6_step3.txt',
'...'
'...'
'...'
'...._80_step20.txt']

I hope my problem is understandable to you. Thank you very much in advance for your help.

Martin

Martin
  • 23
  • 3

2 Answers2

1

As mentioned in the comments, you can specify a function for comparison. The value it produces can be anything comparable - e.g., a tuple of multiple parameters. When comparing tuples, Python first compares first elements; if they're equal, it compares second, etc. So, sorting on a tuple (temperature, step) would do the job.

So, it will be something like:

import re

def temp_and_step(fname):
    """ Extracts temperature and step from a filename like '...._-16_step3.txt'"""
    match = re.search('_(-?\d+)_step(\d+).txdt$', fname)
    if match:
        return int(match.group(1)), int(match.group(2))
    # default value if the file name is not matching the pattern
    return (0, 0)

meas_files.sort(key=temp_and_step)
Marat
  • 15,215
  • 2
  • 39
  • 48
0

I have already solved my problem on my own. realsorted(meas_files) from the natsort package worked perfectly for me. Sometimes the solution is easier than you think. :) Anyways, thank you all for your help and effort.

Martin
  • 23
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 16 '23 at 20:59