Comparing Two Lists Using The Greater Than Or Less Than Operator
I noticed a piece of code recently directly comparing two lists of integers like so: a = [10,3,5, ...] b = [5,4,3, ...,] if a > b: ... which seemed a bit peculiar, but I i
Solution 1:
From Comparing Sequences and Other Types in the Python tutorial:
The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted.
See also the Wikipedia article about lexicographical order.
Solution 2:
The given answers don't account for duplicates in the larger list, you can iterate over the bigger list and slice it each time to compare it with the sub list, this will maintain the order as well as account for duplicates.
This code would work:
defcheck_sublist(List,Sublist)
for i inrange(len(List)):
ifList[i:] ==Sublist:
returnTruereturnFalse
Yes this is not time efficient but this is the only solution I could think since using set() would not maintain the order
Post a Comment for "Comparing Two Lists Using The Greater Than Or Less Than Operator"