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

5
  • Python's sorted function allows you to specify a function to yield what to use as a key for sorting. Commented Jul 13, 2023 at 14:31
  • Why should "..." appear where it does? Commented Jul 13, 2023 at 14:37
  • Do you realize that both versions of meas_files are a list of 1 long string? Commented Jul 13, 2023 at 14:39
  • If you solved your own question, add it as an answer rather than updating the question. This is encouraged on Stack Overflow Commented Jul 13, 2023 at 15:01
  • @ScottHunter: yes, this is true. I have shared with you just a snip of my path because it would be too long. Therefore, I just made a dummy list and forgot the ',' at the end of the list. I have updated my post. Commented Jul 13, 2023 at 15:03

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.