Click here to Skip to main content
15,441,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a list that I would like to sort by numbers first and then by characters. It would be something like this:
['5Yjb', '5kC7', '79jc', '8v2H', '9J6c', 'Fnyq']

In which case I would need to get '79jc', because it is the only one that has 2 numbers. Another example would be:
['0FZXo7p', '0zyBXOF', '1b5HCW3', '3Nar3Ot']

Where it would return '1b5HCW3'. How would I sort this?

What I have tried:

I have tried to count the number of numbers, but that didn't work. Is there a Python build in function that could do something like this. Sorted sorts only by the first number.
Posted
Updated 4-Jan-21 7:53am
Comments
Richard MacCutchan 4-Jan-21 12:33pm    
What exactly are you trying to get in the sorted result?

 
Share this answer
 
v2
Comments
Maciej Los 4-Jan-21 12:43pm    
5ed!
Richard MacCutchan 4-Jan-21 15:43pm    
Thanks Maciej. We, in the UK, have just been put into national lockdown again - stay at home unless essential. How are things in Poland?
Maciej Los 4-Jan-21 16:00pm    
Sadly, but... till 17. January the same.
Quote:
I have a list that I would like to sort by numbers first and then by characters. It would be something like this:
Python
['5Yjb', '5kC7', '79jc', '8v2H', '9J6c', 'Fnyq']

This is the result of standard sort of strings.
You forgot to tell what is wrong with this list.
Quote:
In which case I would need to get '79jc', because it is the only one that has 2 numbers.

You forgot to tell what you want to do with this string.

We can't help you if you don't tell exactly what you want.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900