Sort alphanumeric string list in python
I have a set Show
After sorting I want it to look like
Any idea please
SilentGhost 294k64 gold badges301 silver badges291 bronze badges asked Apr 19, 2010 at 16:21
Jeff Atwood talks about natural sort and gives an example of one way to do it in Python. Here is my variation on it:
Use like this:
Output:
One advantage of this method is that it doesn't just work when the strings are separated by spaces. It will also work for other separators such as the period in version numbers (for example 1.9.1 comes before 1.10.0). answered Apr 19, 2010 at 16:31
Mark ByersMark Byers 782k184 gold badges1552 silver badges1440 bronze badges 4 Short and sweet:
This version:
If you want printed output exactly as described in your example, then:
answered Apr 19, 2010 at 17:32
Daniel StutzbachDaniel Stutzbach 71.4k17 gold badges85 silver badges76 bronze badges 3 You should check out the third party library natsort. Its algorithm is general so it will work for most input.
answered Jul 17, 2014 at 22:01
SethMMortonSethMMorton 42.5k12 gold badges64 silver badges80 bronze badges A simple way is to split up the strings to numeric parts and non-numeric parts and use the python tuple sort order to sort the strings.
answered Apr 19, 2010 at 16:37
Ants AasmaAnts Aasma 51.4k15 gold badges88 silver badges92 bronze badges It was suggested that I repost this answer over here since it works nicely for this case also
Demo:
For Python3 it's necessary to modify it slightly (this version works ok in Python2 too)
answered Jun 6, 2013 at 7:35
John La RooyJohn La Rooy 286k51 gold badges358 silver badges498 bronze badges Generic answer to sort any numbers in any position in an array of strings. Works with Python 2 & 3.
Sample:
Part of the answer is from there
answered Oct 5, 2016 at 23:23
Le DroidLe Droid 4,3142 gold badges34 silver badges32 bronze badges
answered Apr 19, 2010 at 16:25
SilentGhostSilentGhost 294k64 gold badges301 silver badges291 bronze badges Based on SilentGhost's answer:
answered Apr 19, 2010 at 16:31
sets are inherently un-ordered. You'll need to create a list with the same content and sort that. answered Apr 19, 2010 at 16:25
RakisRakis 7,67923 silver badges25 bronze badges 3 For people stuck with a pre-2.4 version of Python, without the wonderful
This does not answer the specific question above ( answered Oct 22, 2013 at 16:11
0
answered Apr 1, 2021 at 17:23
tldrtldr 1063 bronze badges How do you sort a list in alphanumeric order Python?Now there are again two ways of using sorted().. Method #1 : Using key function. # Python3 program to Sort list. # containing alpha and numeric values. def sort(lst): return sorted (lst, key = str ) ... . Method #2 : lambda. # Python3 program to Sort list. # containing alpha and numeric values. def sort(lst):. How do you sort alphanumeric strings?you should split the strings in two first; the other part being the integer part, and the other the string part. then first compare the integers - if the integers are not equal, the string that should appear first is the one with the smaller integer part.
Can you sort a list of string in Python?In Python, there are two ways, sort() and sorted() , to sort lists ( list ) in ascending or descending order. If you want to sort strings ( str ) or tuples ( tuple ), use sorted() .
How do you sort letters in a string alphabetically in Python?Using sorted() with reduce() to sort letters of string alphabetically. Another alternative is to use reduce() method. It applies a join function on the sorted list using '+' operator.
|