how to order the files with increasing order and later merge them

40 Views Asked by At

I have a few files

results_0_1.dat
results_1_1.dat
results_17_1.dat
results_2_1.dat
results_22_1.dat
results_9_1.dat
results_11_1.dat
results_21_1.dat

I'd like to order them in increasing order 0,1,2...9,10,11....20,21,22.....the second number in this case 1 (in bold) it doesn't change because it's the name of the experiment. How could I do it?

I'd be grateful for any help :)

1

There are 1 best solutions below

3
On

The sorted function lets you manually define your sort criteria.

Since you want to sort by the int inbetween the _, we do just that.

sorted(reslist, key=lambda element:int(element.split('_')[1]))