Open In App

Sort the words in lexicographical order in Python

Last Updated : 18 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a strings, we need to sort the words in lexicographical order (dictionary order). Examples :

Input :  "hello python program how are you"
Output :  are
          hello
          how
          program
          python
          you

Input :   "Coders loves the algorithms"
Output :  Coders
          algorithms
          loves
          the

Note: The words which have first letter is capital letter they will print according alphabetical manner.

Approach : Approach used in this program is very simple. Split the strings using split() function. After that sort the words in lexicographical order using sort(). Iterate the words through loop and print each word, which are already sorted.

Python3




# Python program to sort the words in lexicographical
# order
 
def sortLexo(my_string):
 
    # Split the my_string till where space is found.
    words = my_string.split()
     
    # sort() will sort the strings.
    words.sort()
 
    # Iterate i through 'words' to print the words
    # in alphabetical manner.
    for i in words:
        print( i )
 
# Driver code
if __name__ == '__main__':
     
    my_string = "hello this is example how to sort " \
              "the word in alphabetical manner"
    # Calling function
    sortLexo(my_string)


Output :

alphabetical
example
hello
how
in
is
manner
sort
the
this
to
word

Time Complexity: O(nlogn) where n is the length of the string.
Auxiliary Space: O(n)


Similar Reads

Print all numbers up to N in words in lexicographical order
Given an integer N, the task is to print all numbers from 1 to N (0 < N < 100000) in words in lexicographical order. Examples : Input: N = 11Output: eight, eleven, five, four, nine, one, seven, six, three, twoExplanation: The numbers from 1 to N is 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11. Their respective representations in words are {one, two, thr
8 min read
Sort file names in lexicographical order of their extensions
Given an array of strings Files[], representing name of some files, the task is to sort the array based on the lexicographical order of the extensions of the file names. If more than one files have the same extension, then sort them in lexicographically. Examples: Input: files[] = {"ajay.cpp", "pchy.pdf", "loki.docx", "raju.zip" } Output: ajay.cpp,
5 min read
Sort an Array of Strings in Lexicographical order
Given an array of strings arr[] of length N, the task is to sort the strings in Lexicographical order. Examples: Input: arr[] = {"batman", "bat", "apple"} Output: applebat batmanExplanation: The lexicographical order of string is "apple", "bat", "batman" Input: arr[] = {“geeks”, “for”, “geeksforgeeks”} Output: forgeeksgeeksforgeeks Approach 1: The
12 min read
Python | All Permutations of a string in lexicographical order without using recursion
Write a python program to print all the permutations of a string in lexicographical order. Examples: Input : python Output : hnopty hnopyt hnotpy hnotyp hnoypt ...... ytpnho ytpnoh ytpohn ytponh Input : xyz Output : xyz xzy yxz yzx zxy zyx Method 1: Using the default library itertools function permutations. permutations function will create all the
2 min read
Sort all even numbers in ascending order and then sort all odd numbers in descending order
Given an array of integers (both odd and even), sort them in such a way that the first part of the array contains odd numbers sorted in descending order, rest portion contains even numbers sorted in ascending order.Examples: Input: arr[] = {1, 2, 3, 5, 4, 7, 10}Output: arr[] = {7, 5, 3, 1, 2, 4, 10} Input: arr[] = {0, 4, 5, 3, 7, 2, 1}Output: arr[]
15+ min read
Print all distinct circular strings of length M in lexicographical order
Given a string and an integer M, print all distinct circular strings of length M in lexicographical order. Examples: Input: str = "baaaa", M = 3 Output: aaa aab aba baa All possible circular substrings of length 3 are "baa" "aaa" "aaa" "aab" "aba" Out of the 6, 4 are distinct, and the lexicographical order is aaa aab aba baa Input: str = "saurav",
5 min read
Print all the combinations of a string in lexicographical order
Given a string str, print of all the combinations of a string in lexicographical order.Examples: Input: str = "ABC" Output: A AB ABC AC ACB B BA BAC BC BCA C CA CAB CB CBA Input: ED Output: D DE E ED Approach: Count the occurrences of all the characters in the string using a map, then using recursion all the possible combinations can be printed. St
9 min read
Find the k-th string in lexicographical order consisting of n-2 X's and 2 Y's
Given two numbers N and K, the task is to find the Kth string in lexicographical order if the starting string contains (N-2) x's first and then 2 Y'sNote: 1 ? K ? N*(N-1)/2, N*(N-1)/2 are the number of possible permutations Examples: Input : N = 5, K = 7 Output : YXXXY The possible strings in lexicographical order 1. XXXYY 2. XXYXY 3. XXYYX 4. XYXX
6 min read
Generate all numbers up to N in Lexicographical Order
Given an integer N, the task is to print all numbers up to N in Lexicographical order. Examples: Input: N = 15 Output: 1 10 11 12 13 14 15 2 3 4 5 6 7 8 9 Input: N = 19 Output: 1 10 11 12 13 14 15 16 17 18 19 2 3 4 5 6 7 8 9 Approach: In order to solve the problem, follow the steps below: Iterate from 1 to N and store all the numbers in the form of
9 min read
Traverse graph in lexicographical order of nodes using DFS
Given a graph, G consisting of N nodes, a source S, and an array Edges[][2] of type {u, v} that denotes that there is an undirected edge between node u and v, the task is to traverse the graph in lexicographical order using DFS. Examples: Input: N = 10, M = 10, S = 'a', Edges[][2] = { { 'a', 'y' }, { 'a', 'z' }, { 'a', 'p' }, { 'p', 'c' }, { 'p', '
8 min read