python sort strings with leading numbers alphabetically

2024/11/19 14:53:53

I have a list of filenames, each of them beginning with a leading number:

10_file
11_file
1_file
20_file
21_file
2_file ...

I need to put it in this order:

1_file
10_file
11_file
2_file
21_file
22_file ...

If they were just numbers as strings ('1') without the underscore I could sort them with sorted(). I have tried different sort-methods with different key-attributes, also the module "natsort", but with no result. Do I have to write my own algorithm for this? Maybe I could extract the leading numbers and use them for sorting?

UPDATED desired list to correct listing

Answer

Sorting, splitting and list comprehensions work well here.

lst = ['10_file', '11_file', '1_file', '20_file', '21_file', '2_file']lst_split = ['_'.join(x) for x in sorted(i.split('_') for i in lst)]# ['1_file', '10_file', '11_file', '2_file', '20_file', '21_file'] 
https://en.xdnf.cn/q/118533.html

Related Q&A

Javascript is not recognizing a Flask variable

Im passing a set of variables into a Flask template, and I would like to first manipulate them with Javascript. The problem is that when I use the {{ var }} syntax, Javascript isnt recognizing it. The …

Float sum broken? [duplicate]

This question already has answers here:Is floating-point math broken?(36 answers)Closed 9 years ago.print(0.1 + 0.2 == 0.3)returnsFalseWhy?

SyntaxError: EOL while scanning string literal -Python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Formatting a return value from a serial device

I am reading a value from a device over serial, and the return value has the format: [Theoretical position in mm, Encoder position in mm], for example, b\r#-0.001504,-0.001516\n I would like to format …

if Else statement inside for loop is not working [duplicate]

This question already has answers here:Im getting an IndentationError (or a TabError). How do I fix it?(6 answers)Closed 6 months ago.am using following code to check certain conditionsfor myarg in my…

Sending data back and forth from android server to python client

I have posted this few days back but now i ran into another problem after solving that one. DESCRIPTION: working on an android app written in kotlin that behaves as a server side and Python program tha…

Loop to run 4 times to try run a SQL procedure, after 4 attempts then stop

I have attempted to write some code in python to do a loop 4 times. It will fail as spTest doesnt exist. So I want to try loop again (repeated 4 times total) if it still cant find it, I want to break o…

Loops in Python 3.4.3

I apologize ahead of time for my ignorance but I have trying to code something in python that requires a question to be asked to the user and the user responds. Dependent on that response, the program …

Adding userdata on create VM operation with Python SDK for Azure

I am using Python sdk for azure creation virtual machine operation. I want some script to be executed whenever the VM starts. So, I have tried adding the custom-data while creating VM with Python. My d…

python obtain the self variable in another class which already has a self function

I want to use the self variables in one class and use them in another class which already has its own self variables how to do I do this. Some code here to help.class A():self.health = 5 class B(): # T…