1

I have 2 lists:

A:[2 1 2 6 6 7 1 7 5 9 2 6 6 6 6 6 1 0 5 8 8 7 8 1 7 5 4 9 2 9 4 7 6 8 9 4 3]

B:[2 8 2 6 6 7 1 9 8 5 2 2 6 6 6 6 1 0 5 2 8 7 3 4 7 5 4 9 2 9 4 7 6 8 9 4 3]

I want to find the number of same characters and print it in PYTHON. How can i do that? It should also test if the character is the same position with the other list. I am new in Python Can you help?

azro
  • 53,056
  • 7
  • 34
  • 70
Serkan Gün
  • 332
  • 1
  • 2
  • 12
  • 2
    Pls post actual valid Python data structures! Also show the exact desired output for the shown input. And most of all, show your own attempt and where you're stuck. – user2390182 Dec 11 '20 at 22:23
  • 1
    There is no [tag:python] in this question. – Peter Wood Dec 11 '20 at 22:24
  • What would be a valid answer? See [ask] and how to create a [mcve]. – Peter Wood Dec 11 '20 at 22:24
  • How would the algorithm know if something was the same position or not, if it was wrong? – Peter Wood Dec 11 '20 at 22:31
  • Your question is too broad. Welcome to SO. This isn't a discussion forum or tutorial. Please take the [tour] and take the time to read [ask] and the other links found on that page. Invest some time with [the Tutorial](https://docs.python.org/3/tutorial/index.html) practicing the examples. It will give you an idea of the tools Python offers to help you solve your problem. – wwii Dec 11 '20 at 22:39
  • Does [How can I compare two lists in python and return matches](https://stackoverflow.com/questions/1388818/how-can-i-compare-two-lists-in-python-and-return-matches) answer your question? – wwii Dec 11 '20 at 22:41

2 Answers2

5

You can sum whether each element is equal. Equal is True or 1:

count = sum(a == b for a, b in zip(A, B))

zip creates a sequence of pairs, (A[0], B[0]), (A[1], B[1]), (A[2], B[2]), etc.

Peter Wood
  • 23,859
  • 5
  • 60
  • 99
0

You could zip the two lists to get a list of corresponding item pairs, and then count how many of them have two equal items:

num_equal = len([z for z in zip(a,b) if z[0] == z[1]])
Mureinik
  • 297,002
  • 52
  • 306
  • 350