-1

I've been trying to make a function that can take two lists of any size (say, list A and list B) and sees if list B occurs in list A, but consecutively and in the same order. If the above is true, it returns True, else it'll return False

e.g.

A:[9,0,**1,2,3,4,5,6,**7,8] and B:[1,2,3,4,5,6] is successful

A:[1,2,0,3,4,0,5,6,0] and B:[1,2,3,4,5,6] is unsuccessful.

A:[1,2,3,4,5,6] and B [6,5,3,2,1,4] fails because despite having the same 
 numbers, they aren't in the same order

I've tried doing this using nested loops so far and am a bit confused as to where to go

John Diamond
  • 31
  • 1
  • 1
  • 4

6 Answers6

0

i converted the entire list into a string and then found a substring of that string

the list when converted to a string it becomes

str(a)='[9,0,1,2,3,4,5,6,7,8]'

which when when we strip the string becomes

str(a).strip('[]')='9,0,1,2,3,4,5,6,7,8'

Now the problem just converted to

checking if there is a substring in the the string so we can us the in operator to check the substring

The solution

a=[9,0,1,2,3,4,5,6,7,8]
b=[1,2,3,4,5,6]
print(str(b).strip('[]') in str(a).strip(']['))

testcase1

testcase2

Albin Paul
  • 3,330
  • 2
  • 14
  • 30
0

if your arrays are not huge and if you can find a way to map each element in your array to a string you can use:

list1 = [9,0,1,2,3,4,5,6,7,8]
list2 = [1,2,3,4,5,6]

if ''.join(str(e) for e in list2) in ''.join(str(e) for e in list1):
    print 'true'

it just make two string from the lists and than use 'in' to find any accorence

Jaky Bo
  • 23
  • 3
0

Use any function

any(A[i:i+len(B)] == B  for i in range(len(A) - len(B) + 1))

demo

splash58
  • 26,043
  • 3
  • 22
  • 34
0

Just try this:

L1 = [9,0,1,2,3,4,5,6,7,8]
L2 = [1,2,3,4,5,6]
c = 0
w = 0
for a in range(len(L2)):
   for b in range(w+1, len(L1)):
      if L2[a] == L1[b]:
        c = c+1
        w = b
        break
      else:
        c = 0
    if c == len(L2):
       print('yes')
       break

Here you check if the element of l2 is in l1 and if so breaks the first loops remember where you left and of the next element of l2 is the same as the next element of l1 and so on.

And the last part is to check if this happened as much times as the length of l2. if so then you know that the statement is correct!

Seppe Mariën
  • 355
  • 1
  • 13
0

Try this:

L1 = [9,2,1,2,0,4,5,6,7,8]
L2 = [1,2,3,4,5,6]
def sameorder(L1,L2):
    for i in range(len(L1)-len(L2)+1):
        if L1[i:len(L2)+i]==L2:
            return True
    return False
Artier
  • 1,648
  • 2
  • 8
  • 22
0

You can create sublists of a that can be analyzed:

def is_consecutive(a, b):
   return any(all(c == d for c, d in zip(b, i)) for i in [a[e:e+len(b)] for e in range(len(a)-len(b))])

cases = [[[9, 0, 1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6]], [[1, 2, 0, 3, 4, 0, 5, 6, 0], [1, 2, 3, 4, 5, 6]], [[1, 2, 3, 4, 5, 6], [6, 5, 3, 2, 1, 4]]]
final_cases = {"case_{}".format(i):is_consecutive(*a) for i, a in enumerate(cases, start=1)}    

Output:

{'case_3': False, 'case_2': False, 'case_1': True}
Ajax1234
  • 69,937
  • 8
  • 61
  • 102