0

I am using Python 3 and I have this problem:

ListA = [38,40,27,11,1,5,22,7,47,3,11]
ListB = [12,16,38,5,40,27,3]

I need to count how many times in a row any numbers from ListB appeared in ListA. The output, in this case, should be 3. Because 38, 40 and 27 are numbers that are in ListB, and are first 3 numbers in ListA.

In case of no matches (if the first number in ListA is not in ListB), the output should be: 0.

Georgy
  • 12,464
  • 7
  • 65
  • 73
  • Output should be 1, because B doesnt have 30, if it was A=[12,12,30,14,12] and B=[12], output should be 2 – Vitali Kazinski Oct 04 '18 at 08:59
  • yes I got it now, I misunderstood the question at first glance. – Tanmay jain Oct 04 '18 at 09:00
  • 1
    This question is unclear. How do you calculate `3`? Why are 38 / 40 / 27 in scope but not 5 / 3 ? – jpp Oct 04 '18 at 09:02
  • Because numbers 11 and 1 are not in ListB, and i want to get result of how many times -in a row- any numbers from ListB showed in ListA – Vitali Kazinski Oct 04 '18 at 09:05
  • do you want to find maximum times a number appeared in a row ? ` A =[12,12,15,12,12,12]` and `B = [12]` ans is 3 or ...ans should be 2 because 12 appears two times consecutively. – Tanmay jain Oct 04 '18 at 09:08
  • No, i just want to find how many times in a row numbers from ListB apeard in ListA, starting from ListA[0:]. In that case that you gave, output should be 2, yes! – Vitali Kazinski Oct 04 '18 at 09:10
  • Please amend your question with the clarifications / examples you provided in your comments. – planetmaker Oct 04 '18 at 10:22
  • @planetmaker someone edited my question, I typed exacly what i wanted b4 – Vitali Kazinski Oct 04 '18 at 12:14
  • Well, neither edit really made the question clearer to me. From the accepted answer, you are looking for *how many leading entries from ListA are found in ListB*. Is that correct? Thus you start to look through listA at the start. You go and look whether that item is in listB. And then you continue towards the end of listA and stop when you hit an element in A which you do not find in listB. That problem is not well explained so far, IMHO :) Especially that you are looking only at the start of listA and stop iteration when you find a non-match – planetmaker Oct 04 '18 at 13:05
  • Ok, look. I have main list where i put numbers - ListA. Than i have another list, which is fixed (i cannot change it). My goal is to find how many times in a row i entered numbers that are in the second, unchangable list, ListB. So, in order for accepted answer to work, i wrote formula: ListA.insert(0,input("insert new number") because when i enter new number, it puts it at the beginning of the ListA. Hope i clarified things now – Vitali Kazinski Oct 04 '18 at 14:13

4 Answers4

1

Something like

counter = 0
for value in A:
    if value in B:
        counter += 1
    else:
        break
blue_note
  • 27,712
  • 9
  • 72
  • 90
1

You can do something like this:

ListA =[38,40,27,11,1,5,22,7,47,3,11]
ListB = [12,16,38,5,40,27,3]

count = 0
for item_a in ListA:
    if item_a in ListB:
        count += 1
    else:
        break

print(count)  
#  3       

It only increases the counter if the element is present at front of ListA and found in ListB else breaks out of loop.

BlackBeard
  • 10,246
  • 7
  • 52
  • 62
1

A very concise way of doing this would be by using itertools.takewhile function:

from itertools import takewhile

ListA = [38,40,27,11,1,5,22,7,47,3,11]
ListB = [12,16,38,5,40,27,3]

sum(1 for _ in takewhile(ListB.__contains__, ListA))
# 3

takewhile will take items from ListA as long as they are contained in ListB (38,40,27). And the sum(1 for _ in <iterable>) will simply count them.

Georgy
  • 12,464
  • 7
  • 65
  • 73
0

You can convert ListB to a set first for efficient membership lookup:

setB = set(ListB)
for count, a in enumerate(ListA):
    if a not in setB:
        break
else:
    count = len(ListA)

With your sample input, count would become: 3

blhsing
  • 91,368
  • 6
  • 71
  • 106