Python:如何按多个条件对字符串(文件名)进行排序?

huangapple go评论66阅读模式
英文:

Python: How to sort strings (filenames) by multiple conditions?

问题

我有一个关于对存储在列表中的字符串进行排序的问题。这些字符串实际上是测量的路径/文件名,例如:

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

  1. -10 代表测量的温度。温度范围从 -20 到 80 °C(之间有 2 °C 的步进) -> 第一条件 我想按从 -20 到 80 的顺序对字符串进行排序。

  2. step1 表示每个温度下的测试编号。在每个温度下,我至少执行 20 次测试 -> 第二条件 对字符串进行排序,从 1 到 20。

我的列表应该如下所示:

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']

我尝试了以下方法:list(Tcl().call('lsort', '-dict', meas_files))(我用于按步骤编号对列表进行排序的一条命令),但结果是对步骤编号进行了正确排序,但它从 -2 开始到 -20,然后继续从 0 到 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']

我希望您能理解我的问题。非常感谢您提前的帮助。

Martin

英文:

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

答案1

得分: 1

正如评论中提到的,您可以为比较指定一个函数。它产生的值可以是可比较的任何东西,例如多个参数的元组。在比较元组时,Python 首先比较第一个元素;如果它们相等,然后比较第二个,依此类推。因此,对于元组(temperature, step)的排序可以完成任务。

因此,代码会是这样的:

import re

def temp_and_step(fname):
    """从文件名中提取温度和步骤,例如'...._-16_step3.txt'"""
    match = re.search(r'_(-?\d+)_step(\d+).txt$', fname)
    if match:
        return int(match.group(1)), int(match.group(2))
    # 如果文件名不匹配模式,则返回默认值
    return (0, 0)

meas_files.sort(key=temp_and_step)
英文:

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)

答案2

得分: 0

我已经自己解决了我的问题。natsort 包中的 realsorted(meas_files) 对我来说完美地起作用了。有时解决方案比你想象的要简单。:) 无论如何,感谢大家的帮助和努力。

英文:

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. Python:如何按多个条件对字符串(文件名)进行排序? Anyways, thank you all for your help and effort.

huangapple
  • 本文由 发表于 2023年7月13日 22:27:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定