Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could start with something like:</p> <pre><code>set(lst1).issubset(lst2) </code></pre> <p>to see if lst1 is contained within lst2 ignoring order. If it passes the test that one list is contained within the other, then you could do something like:</p> <pre><code>ii = lst2.index(lst1[0]) if lst2[ii:ii+len(lst1)] == lst1: return True else: return False </code></pre> <p>Originally I stated that the first check was irrelevant given the second, although you'd have to properly handle the <code>ValueError</code> if the first element of lst1 is not in lst2. </p> <p><strong>Edit:</strong> Just as a side note, I compared a version of my code vs yan's and mine is significantly faster under almost all use cases, especially if len(lst1) is larger (up to 200x speedup vs yan's implementation). Give it a try with the <code>timeit</code> module.</p> <pre><code>def check(lst1,lst2): try: ii = lst2.index(lst1[0]) except ValueError: return False if lst2[ii:ii+len(lst1)] == lst1: return True else: return False </code></pre> <p>As an explanation of how it works <code>ii = lst2.index(lst1[0])</code> finds the index in <code>lst2</code> that matches the first element of <code>lst1</code>. If that item is missing from <code>lst2</code> it catches the <code>ValueError</code> and returns <code>False</code>. If that element does exist, <code>lst2[ii:ii+len(lst1)] == lst1</code> compares all of <code>lst1</code> vs the sublist of <code>lst2</code> starting from the matched element and taking the next <code>len(lst)</code> elements. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload