# Python program to list Sort by Number value in String

• Last Updated : 18 Jul, 2021

Given a List of strings, the task is to write a Python program to sort list by the number present in the Strings. If no number is present, they will be taken to the front of the list.

Input : test_list = [“gfg is 4”, “all no 1”, “geeks over 7 seas”, “and 100 planets”]
Output : [‘all no 1’, ‘gfg is 4’, ‘geeks over 7 seas’, ‘and 100 planets’]
Explanation : 1 < 4 < 7 < 100, numbers in strings deciding order.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Input : test_list = [“gfg is 4”, “geeks over 7 seas”, “and 100 planets”]
Output : [‘gfg is 4’, ‘geeks over 7 seas’, ‘and 100 planets’]
Explanation : 4 < 7 < 100, numbers in strings deciding order.

Method 1 : Using sort(), split() and isdigit()

In this, we perform the task of in-place sorting using sort(), and perform task of getting number from string using split() and final detection is done using isdigit().

Example:

## Python3

 `import` `sys ` ` `  `def` `num_sort(strn): ` ` `  `    ``# getting number using isdigit() and split() ` `    ``computed_num ``=` `[ele ``for` `ele ``in` `strn.split() ``if` `ele.isdigit()] ` ` `  `    ``# assigning lowest weightage to strings  ` `    ``# with no numbers ` `    ``if` `len``(computed_num) > ``0``: ` `        ``return` `int``(computed_num[``0``]) ` `    ``return` `-``1` ` `  ` `  `# initializing Matrix ` `test_list ``=` `[``"gfg is"``, ``"all no 7"``, ``"geeks over seas"``, ``"and planets 5"``] ` ` `  `# printing original list ` `print``(``"The original list is : "` `+` `str``(test_list)) ` ` `  `# performing sort ` `test_list.sort(key``=``num_sort) ` ` `  `# printing result ` `print``(``"Sorted Strings : "` `+` `str``(test_list)) `

Output:

The original list is : [‘gfg is’, ‘all no 7’, ‘geeks over seas’, ‘and planets 5’]

Sorted Strings : [‘gfg is’, ‘geeks over seas’, ‘and planets 5’, ‘all no 7’]

Method 2 : Using sorted(), lambda,  split() and isdigit()

In this, lambda function is used to inject sort functionality performed using sorted(). Rest each process is similar to above explained method.

Example:

## Python3

 `# initializing Matrix ` `test_list ``=` `[``"all no 100"``, ``"gfg is"``, ``"geeks over seas 62"``, ``"and planets 3"``] ` ` `  `# printing original list ` `print``(``"The original list is : "` `+` `str``(test_list)) ` ` `  `# performing sorting ` `# lambda function injecting functionality ` `res ``=` `sorted``(test_list, key``=``lambda` `strn: ``-``1`  `             ``if` `len``([ele ``for` `ele ``in` `strn.split()  ` `                     ``if` `ele.isdigit()]) ``=``=` `0`  `             ``else` `int``([ele ``for` `ele ``in` `strn.split()  ` `                       ``if` `ele.isdigit()][``0``])) ` ` `  `# printing result ` `print``(``"Sorted Strings : "` `+` `str``(res)) `

Output:

The original list is : [‘all no 100’, ‘gfg is’, ‘geeks over seas 62’, ‘and planets 3’]

Sorted Strings : [‘gfg is’, ‘and planets 3’, ‘geeks over seas 62’, ‘all no 100’]

My Personal Notes arrow_drop_up
Recommended Articles
Page :