How to find elements in one arbitrary list that occur in another list (preserving their order)?

159 Views Asked by At

I need to create an algorithm which would read user input of lists A and B and determine whether elements from list B occur in list A (if they occur, program needs to print 'Yes', and 'No' otherwise').

I have come up with the following code which should can be a starting point:

n=int(input('Enter the length of list A '))
A=[]
for i in range (0,n):
    InpEl=int(input('Enter the elements '))
    A.append(InpEl)
print(A)
n=int(input('Enter the length of list B '))
B=[]
for i in range (0,n):
    InpEl2=int(input('Enter the elements '))
    B.append(InpEl2)
print(B)

checklist=B
for each in A:
    if each in checklist:
        print('YES')
    else:
         print('NO')

Although in any case, I get 'No'. What is the mistake here?

Also, later I may need to modify the list so the program could determine if elements of B occur in A in the order they appear in B, but not necessarily consecutively.

For example, let M be the length of B and N be the length of A.
Then the program should return yes if there are indices i0, i1...im+1 such that 0<= i0 < i1...< im-1 < N such that A[i0] = B[0];A[i1] = B[1]...A[im-1] =
B[m-1].

Is there a simpler way to build the loop which would satisfy this kind of request?

P.S.: is it possible to make user input read not only integers, but strings? I am not sure if raw_input would be useful in Python 3.5.

P.S.S: sorry, I made a minor mistake inputting the code here, I fixed it now. Another question: I get the output of multiple yes' and no's for each element:

Enter the length of list A 3
Enter the elements 1
Enter the elements 2
Enter the elements 3
[1, 2, 3]
Enter the length of list B 3
Enter the elements 5
Enter the elements 4
Enter the elements 3
[5, 4, 3]
NO
NO
YES

How can I modify the code so it would print only one yes and no only once in case of any occurencies happen?

2

There are 2 best solutions below

0
wkzhu On

Here's one solution. Keep in mind there are many that have asked this type of question before and that best practice is to search around before asking.

a = input('enter list A with comma between each element: ')
b = input('enter list B with comma between each element: ')

a = a.split(',')
b = b.split(',')

contained_in_b = [element in b for element in a]

for i, in_b in enumerate(contained_in_b):
    print('element {} contained in list B: {}'.format(a[i], in_b))

Better to take the raw input all together and use Python to split it into lists. This way, no need for the user to give the length of the list beforehand. Also, no need to convert to int - string comparisons work fine.

contained_in_b uses a list comprehension - a handy feature of Python that applies the boolean element in b to each element in a. Now you have a list of True/False values that you can enumerate through to print out the desired output.

0
Prune On

One weapon you get is the all operator, which just checks that all of the items in the iterable are True:

A = [1, 4, 6, 8, 13]
B = [4, 6, 13, 8]
C = [3, 8, 25]

master = [i for i in range(20)]

print all(i in master for i in A)
print all(i in master for i in B)
print all(i in master for i in C)

Output:

True
True
False

To get the order as well, you'll need to drop back to an iterative method, stepping through the first list with a loop, while you maintain an index to know where you are in the second. For each value in the first list, go through the rest of the second list, until you either find the item (temporary success) or hit the end (failure).

Reading in number names and converting them to integers is a separate problem, and longer code.