w3resource

Python: Find the numbers of a given string and store them in a list, display the numbers which are bigger than the length of the list in sorted form


Write a Python program to find the numbers in a given string and store them in a list. Afterward, display the numbers that are longer than the length of the list in sorted form. Use the lambda function to solve the problem.

Sample Solution:

Python Code :

# Define a string containing alphanumeric characters and numbers
str1 = "sdf 23 safs8 5 sdfsd8 sdfs 56 21sfs 20 5"

# Display the original string
print("Original string: ", str1)

# Split the string into a list of substrings separated by spaces
str_num = [i for i in str1.split(' ')]

# Calculate the length of the list of substrings
lenght = len(str_num)

# Extract and sort the numbers found within the substrings, converting them to integers
numbers = sorted([int(x) for x in str_num if x.isdigit()])

# Print the sorted numbers that are greater than the length of the list of substrings
print('Numbers in sorted form:')
for i in (filter(lambda x: x > lenght, numbers)):
    print(i, end=' ') 

Sample Output:

Original string:  sdf 23 safs8 5 sdfsd8 sdfs 56 21sfs 20 5
Numbers in sorted form:
20 23 56 

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to find all anagrams of a string in a given list of strings using lambda.
Next: Write a Python program that multiply each number of given list with a given number using lambda function. Print the result.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.