-3

Assuming I have these two lists:

a = [1,0,0,0,1,1,1,1,1,1,1,1,0,0]

b = [0,0,0,0,1,1,1,0,0,0,1,1,0,0]

I would like to know the number of times the number 1 appears in the same position, in this case it would be 5

miguel
  • 9
  • 3
  • 3
    Please show us what you have tried so far – tomjn Mar 25 '21 at 15:58
  • 1
    You can and the two lists and then count the ones in the result. – KillerToilet Mar 25 '21 at 16:03
  • Haven't been able to think of anything yet... The suggestion is for a similar question, but they want the values that appear in both lists, here I'm trying to obtain the number ov values that are the same and in the same position – miguel Mar 25 '21 at 16:06
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). “I can't think of anything” is an issue for local tutelage in problem analysis, not a Stack Overflow issue. We expect you to make an honest attempt, and *then* ask a *specific* question about your algorithm or technique. Stack Overflow is not intended to replace existing documentation and tutorials. – Prune Mar 25 '21 at 16:12

3 Answers3

0

Use bitwise operators and zip:

sum([x&y for x,y in zip(a,b)])
Chris
  • 15,819
  • 3
  • 24
  • 37
-1

Here is what I was suggesting in my comment.

import numpy as np

a = [1,0,0,0,1,1,1,1,1,1,1,1,0,0]

b = [0,0,0,0,1,1,1,0,0,0,1,1,0,0]

result = np.logical_and(np.array(a), np.array(b))

print(np.count_nonzero(result == True))
KillerToilet
  • 196
  • 9
-2

Make the sum of the two lists then see if the result of each cell is 2 (1 + 1):

a = [1,0,0,0,1,1,1,1,1,1,1,1,0,0]

b = [0,0,0,0,1,1,1,0,0,0,1,1,0,0]

sum = [] # sun of the two lists
for x in range(len(a)):
    sum.append(a[x] + b[x])

_1_in_both = []
for x in range(len(sum)):
    if sum[x] == 2: # sum[x] == 2 <==> 1 in both lists
        _1_in_both.append(x)

print('1 occurs in both lists', len(_1_in_both), 'times, in positions', _1_in_both)

Output:

1 occurs in both lists 5 times, in positions [4, 5, 6, 10, 11]

D_00
  • 1,440
  • 2
  • 13
  • 32