Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python lexicographical comparison

# Note that a string x is lexicographically smaller than string y 
# if x comes before y in dictionary order, that is, 
#    either x is a prefix of y, 
#    or if i is the first position such that x[i] != y[i], 
#          then x[i] comes before y[i] in alphabetic order
from operator import lt, gt

def compare(string1, string2, less=True):
    op = lt if less else gt
    for char1, char2 in zip(string1, string2):
        ordinal1, ordinal2 = ord(char1), ord(char1)
        if ordinal1 == ordinal2:
            continue
        else:
            return op(ordinal1, ordinal2)
    return op(len(string1), len(string2))
Comment

PREVIOUS NEXT
Code Example
Python :: how to seperate words and number in a list 
Python :: python get response headers 
Python :: cyclically rotate an array by one 
Python :: how to get all folders on path in python 
Python :: pandas remove item from dictionary 
Python :: execute command in python script 
Python :: dataframe fillna with 0 
Python :: python palindrome string 
Python :: How to Convert Strings to Datetime in Pandas DataFrame 
Python :: remove first 2 rows in pandas 
Python :: python list of all characters 
Python :: remove all rows without a value pandas 
Python :: colab add package 
Python :: list of df to df 
Python :: how to create a new virtualenv 
Python :: save a file as a pickle 
Python :: how to add subplots for histogram 
Python :: read live video from usb opencv python 
Python :: python get nearest value in list 
Python :: matplotlib plot 2d point 
Python :: make sure text is on page selenium python 
Python :: pandas concatenate 
Python :: show image python 
Python :: convert string in list format to list python 
Python :: add pip to path 
Python :: convex hull algorithm python 
Python :: stdout.write python 
Python :: how to translate to string to different alphabet python 
Python :: django change user password 
Python :: python datetime weekday 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =