Search code examples
pythonnumpyrow

Find row number for specific change in python numpy


I have a file like this:

C
C
C
C
C
C
C
C
B
C
C
C
C
C
C
C
C
C         
C        
C         
C        
C       
C
C            
C
C
B   
B   
B
B   
B   

I like to print the row number where there is a change from C to B. Like here in row 9 and 27. I need to row number only. How to do that in numpy python.

Thank you


Solution

  • This is one possible solution, but it's not using Numpy

    f = open('yourfile.txt', 'r')
    x = 0
    for i in f:
       x = x + 1
       if i == 'B':
          print('row' + str(x))