Find list in Python list

Asked 2 years ago, Updated 2 years ago, 13 views

Hello, this is my first question since I joinedHaha For example, you need to create a Python code

A = [1,3,5,2,3,4,6,6,7,8] B = [4,6,6]

If you have these two lists, you have to create a program that checks that the numbers in the A list are in the order that the numbers in the B list are arranged What I have just made the code is

//def find(Alist,Blist):
  for i in Alist:
    if Blist in Alist:
      for i in range(len(Blist)):
        sequence = Blist[i+1]-Blist[i]
        if sequence == 1:
          return True
        else:
          return False
    else:
      return False

#--------------------------------------------------------------------------------------------
A = [1,2,3,4,5,6,7,8,9]
B = [1,2,3]

findS = find(A,B)
print(findS)

It's this, but it keeps saying false... I'm trying to make it using functions and loops, but it doesn't work well I ask for your help!

python

2022-09-21 22:11

1 Answers

A string indicates whether it is included or not included.

'abc' in 'aaaaaabcccccc'
True

'abcd' in 'aaaaaabcccccc'
False

The simplest way is to convert the list into a string and conduct a membership test as shown below.

A = [1,3,5,2,3,4,6,6,7,8]

B = [4,6,6]

''.join(map(str, B)) in ''.join(map(str, A))
Out[14]: True


2022-09-21 22:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.