Lexicographical Order in Python

Lexicographical Order in Python

  1. Lexicographic Order in Python
  2. Sorting Numeric Lists Into Lexicographic Order in Python

We will introduce lexicographic order in Python. We’ll also discuss different methods to achieve lexicographic order with examples.

Lexicographic Order in Python

In math, the lexicographic or lexicographical order is the process of ordering a list of elements or an array of elements that are arranged alphabetically. The other term used for lexicographic order is dictionary order.

The lexicographic order has a variety of forms and generalizations that can be used. In simple words, lexicographic ordering is sorting words from a list or array based on their first letters.

If the initial letter is identical, the second letter is utilized to order the words. We may come across some situations in which we need to sort the data according to our requirements, and we’ll use lexicographical order to sort the data.

Let’s discuss lexicographic order with examples to understand it better. As shown below, we will create a sample list containing the names of some random things that we will sort using the sort() method.

Code:

# python
sampleData = ["Egg", "Milkshake", "Banana", "Apple"]
print("List before using Lexicographical Order: ", sampleData)

sampleData.sort()
print("List after using Lexicographical Order: ", sampleData)

Output:

lexicographical order in python using sort()

Sorting the list above is done in alphabetical order.

In this next example, we’ll use a string instead of a list to apply lexicographic order. We will use the split() function to convert a string into a list and then use the sort() function.

Code:

# python
def LexicographicSort(sampleData):
    Data = sampleData.split()
    Data.sort()
    for a in Data:
        print ( a )

sampleData= "Let's try to sort this string into Lexicographical order"
print ("String before using Lexicographical Order: ", sampleData)
print ("String after using Lexicographical Order: ")

LexicographicSort(sampleData)

Output:

lexicographical order in python using string with sort() and split()

Using the sort() and split() functions we can sort the strings into lexicographic order.

Sorting Numeric Lists Into Lexicographic Order in Python

We can also sort numerical lists using the sort() function. In this example, we will use a random array of numbers that we can sort using the sort() function.

Code:

# python
def LexicographNumberSort(RangedNum):
    RangedNum.sort()
    print (RangedNum)

RangedNum = [1, 4, 5, 3, 10, 16, 12]
print ("Sorted numbers: ")
LexicographNumberSort(RangedNum)

Output:

lexicographical order in python using numeric array with sort()

The sort() method can sort a list of numbers in lexicographic order.

Rana Hasnain Khan avatar Rana Hasnain Khan avatar

Rana is a computer science graduate passionate about helping people to build and diagnose scalable web application problems and problems developers face across the full-stack.

LinkedIn

Related Article - Python Math

  • Calculate Factorial in Python
  • Calculate Inverse of Cosine in Python
  • Calculate Modular Multiplicative Inverse in Python
  • Fit Poisson Distribution to Different Datasets in Python
  • Reduce Fractions in Python
  • Define an Infinite Value in Python