
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to print sorted number formed by merging all elements in array
When it is required to print the sorted numbers that are formed by merging the elements of an array, a method can be defined that first sorts the number and converts the number to an integer. Another method maps this list to a string, and is sorted again.
Example
Below is a demonstration of the same
def get_sorted_nums(my_num): my_num = ''.join(sorted(my_num)) my_num = int(my_num) print(my_num) def merged_list(my_list): my_list = list(map(str, my_list)) my_str = ''.join(my_list) get_sorted_nums(my_str) my_list = [7, 845, 69, 60, 99, 11] print("The list is :") print(my_list) print("The result is :") merged_list(my_list)
Output
The list is : [7, 845, 69, 60, 99, 11] The result is : 11456678999
Explanation
A method named ‘get_sorted_nums’ is defined that takes a number as a parameter.
It is first converted to a string and then sorted.
Next, it is converted back to an integer and displayed on the console.
Another method named ‘merged_list’ is defined that takes a list as a parameter.
It is converted into a string using the ‘map’ method and then converted to a list.
The previous method to sort and convert to integer is again called by passing this string.
Outside the method, a list is defined and is displayed on the console.
The method is called by passing this parameter.
The output is displayed on the console.
- Related Articles
- To print all elements in sorted order from row and column wise sorted matrix in Python
- Python program to print all distinct elements of a given integer array.
- JavaScript Program for Print all triplets in sorted array that form AP
- Merging two sorted arrays into one sorted array using JavaScript
- Print sorted distinct elements of array in C language
- Python Program to Print all Numbers in a Range Divisible by a Given Number
- Program to find intervals by merging target interval in Python
- Python program to print the duplicate elements of an array
- Python program to print all the common elements of two lists.
- Print all triplets in sorted array that form AP in C++
- Program to find minimum number colors remain after merging in Python
- Python program to print the elements of an array in reverse order
- Java program to print all distinct elements of a given integer array in Java
- C# program to print all distinct elements of a given integer array in C#
- Python program to copy all elements of one array into another array
