Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import deque
- def buscar(lineas, patron, historial=5):
- lineas_anteriores = deque(maxlength=historial)
- for linea in lineas:
- if pattern in linea:
- yield linea, lineas_anteriores
- lineas_anteriores.append(linea)
- if __name__ == "__main__":
- with open ('libro_python.txt') as f:
- for linea, lineas_anteriores in buscar(f, 'python', 5):
- for linea_anterior in lineas_anteriores:
- print(linea_anterior, end='')
- print(linea, end='')
- print('-' * 20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement