Home > Enterprise >  How to find at which line is there a match for list comparison (PYTHON)
How to find at which line is there a match for list comparison (PYTHON)

Time:10-31

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

B = ['0', '4', '1', '5']

Say I want to find out at which line does B equal to the list. How do I write a solution for this?

The answer would be the third line.

I tried doing a for loop.

CodePudding user response:

You can try list.index(element) to get the index of the element in the original list (A). In your terminology, to get the line just add one to the index.

line = A.index(B)   1

CodePudding user response:

you dont need use loops.you can get the element index by indexing.

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

B = ['0', '4', '1', '5']

print(A.index(B))

>>> 2
  • Related