Sort words in lexicographic order in Python

lexicographic order | Python Methods and Functions


 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: words that have the first letter are capital letters and will be printed in alphabetical order.

The approach used in this program is very simple. Split the lines using the split () function. After that, sort the words in lexicographic order using sort (). Repeat words in a loop and print each word that is already sorted.

# Python program for sorting words in lexicographic
# order


def sortLexo (my_string):


  # Split string my_string until space is found.

  words = my_string.split ()


# sort () will sort the lines.

words.sort ()


  # I iterate through "words" to print words

# in alphabetical order.

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"

# Function call

sortLexo (my_string)


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

Sort words in lexicographic order in Python: StackOverflow Questions

Answer #1

Check out product() in the itertools module. It does exactly what you describe.

import itertools

my_list = [1,2,3,4]
for pair in itertools.product(my_list, repeat=2):

This is equivalent to:

my_list = [1,2,3,4]
for x in my_list:
    for y in my_list:
        foo(x, y)

Edit: There are two very similar functions as well, permutations() and combinations(). To illustrate how they differ:

product() generates every possible pairing of elements, including all duplicates:

1,1  1,2  1,3  1,4
2,1  2,2  2,3  2,4
3,1  3,2  3,3  3,4
4,1  4,2  4,3  4,4

permutations() generates all unique orderings of each unique pair of elements, eliminating the x,x duplicates:

 .   1,2  1,3  1,4
2,1   .   2,3  2,4
3,1  3,2   .   3,4
4,1  4,2  4,3   .

Finally, combinations() only generates each unique pair of elements, in lexicographic order:

 .   1,2  1,3  1,4
 .    .   2,3  2,4
 .    .    .   3,4
 .    .    .    .

All three of these functions were introduced in Python 2.6.

Answer #2

From the python 2 manual:

CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.

When you order two strings or two numeric types the ordering is done in the expected way (lexicographic ordering for string, numeric ordering for integers).

When you order a numeric and a non-numeric type, the numeric type comes first.

>>> 5 < "foo"
>>> 5 < (1, 2)
>>> 5 < {}
>>> 5 < [1, 2]

When you order two incompatible types where neither is numeric, they are ordered by the alphabetical order of their typenames:

>>> [1, 2] > "foo"   # "list" < "str" 
>>> (1, 2) > "foo"   # "tuple" > "str"

>>> class Foo(object): pass
>>> class Bar(object): pass
>>> Bar() < Foo()

One exception is old-style classes that always come before new-style classes.

>>> class Foo: pass           # old-style
>>> class Bar(object): pass   # new-style
>>> Bar() < Foo()

Is this behavior mandated by the language spec, or is it up to implementors?

There is no language specification. The language reference says:

Otherwise, objects of different types always compare unequal, and are ordered consistently but arbitrarily.

So it is an implementation detail.

Are there differences between any of the major Python implementations?

I can"t answer this one because I have only used the official CPython implementation, but there are other implementations of Python such as PyPy.

Are there differences between versions of the Python language?

In Python 3.x the behaviour has been changed so that attempting to order an integer and a string will raise an error:

>>> "10" > 5
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    "10" > 5
TypeError: unorderable types: str() > int()