Advertisement
Fhernd

historial-lineas.py

Mar 18th, 2018
1,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.50 KB | None | 0 0
  1. from collections import deque
  2.  
  3. def buscar(lineas, patron, historial=5):
  4.     lineas_anteriores = deque(maxlength=historial)
  5.    
  6.     for linea in lineas:
  7.         if pattern in linea:
  8.             yield linea, lineas_anteriores
  9.        
  10.         lineas_anteriores.append(linea)
  11.        
  12. if __name__ == "__main__":
  13.     with open ('libro_python.txt') as f:
  14.         for linea, lineas_anteriores in buscar(f, 'python', 5):
  15.             for linea_anterior in lineas_anteriores:
  16.                 print(linea_anterior, end='')
  17.                
  18.             print(linea, end='')
  19.             print('-' * 20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement