Comparing the order of two Python lists

I am looking for some help comparing the order of 2 Python lists, list1 and list2 , to detect when list2 has failed.

  • list1 is static and contains strings a,b,c,d,e,f,g,h,i,j. This is the "correct" order.
  • list2 contains the same lines, but the order and number of lines may change. (e.g. a,b,f,d,e,g,c,h,i,jor a,b,c,d,e)

I am looking for an effective way to detect when list2 is our order, comparing it with list1 .

For example, if list2 is a,c,d,e,g,isupposed to return true (as the strings are in order)

For now, list2 a,d,b,c,e should return false (since line d is displayed out of order)

+4
source share
6 answers

First we define list1:

>>> list1='a,b,c,d,e,f,g,h,i,j'.split(',')
>>> list1
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

As long as yours list1is in alphabetical order, we will not allow this. This code works independently.

Now create list2that failed:

>>> list2 = 'a,b,f,d,e,g,c,h,i,j'.split(',')
>>> list2
['a', 'b', 'f', 'd', 'e', 'g', 'c', 'h', 'i', 'j']

Here's how to check if it works list2:

>>> list2 == sorted(list2, key=lambda c: list1.index(c))
False

False means out of order.

Here is an example that is fine:

>>> list2 = 'a,b,d,e'.split(',')
>>> list2 == sorted(list2, key=lambda c: list1.index(c))
True

True means order is ok.

Ignoring list items1 not in list2

Consider a list2, which has an element not in list1:

>>> list2 = 'a,b,d,d,e,z'.split(',')

To ignore an unwanted element, create list2b:

>>> list2b = [c for c in list2 if c in list1]

:

>>> list2b == sorted(list2b, key=lambda c: list1.index(c))
True

sorted

>>> list2b = ['a', 'b', 'd', 'd', 'e']
>>> indices = [list1.index(c) for c in list2b]
>>> all(c <= indices[i+1] for i, c in enumerate(indices[:-1]))
True
+7

list1, list1 ? ?

def is_sorted(alist):
    return alist == sorted(alist)

print is_sorted(['a','c','d','e','g','i'])
# True

print is_sorted(['a','d','b','c','e'])
# False
+3

, . , list1 10 , list2 - , index .

preprocess list1, . ( list2 s, , , , list2):

list1_indices = {item: i for i, item in enumerate(list1)}

, list2 list1, list2:

is_sorted = all(list1_indices[x] < list1_indices[y] for x, y in zip(list2, list2[1:]))

itertools.izip itertools.islice, zip, , , list2 :

# On Python 3, we can just use zip. islice is still needed, though.
from itertools import izip, islice
is_sorted = all(list1_indices[x] < list1_indices[y]
                for x, y in izip(list2, islice(list2, 1, None)))
+1
is_sorted = not any(list1.index(list2[i]) > list1.index(list2[i+1]) for i in range(len(list2)-1))

any true, - . , list2 , 1.

0
if list2 == sorted(list2,key=lambda element:list1.index(element)):
    print('sorted')
0

Suppose that when you write this list1, these are lines a, b, c, d, e, f, g, h, i, which means that a may be a “zebra” and row b may be an “elephant”, therefore, order cannot be in alphabetical order. In addition, this approach will return false if the item is in list2 but not in list1.

good_list2 = ['a','c','d','e','g','i']

bad_list2 = ['a','d','b','c','e']

def verify(somelist):
    list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
    while len(list1) > 0:
        try:
            list1 = list1[:list1.index(somelist.pop())]
        except ValueError:
            return False
    return True
0
source

All Articles