Python nested loop - get next N lines

I am new to Python and trying to execute a nested loop. I have a very large file (1.1 million lines) and I would like to use it to create a file with each line along with the next N lines, for example, with the following three lines:

1    2
1    3
1    4
2    3
2    4
2    5

Right now I'm just trying to get loops working with rownumbers instead of strings, as this is easier to visualize. I came up with this code, but it does not behave the way I want:

with open('C:/working_file.txt', mode='r', encoding = 'utf8') as f: 
for i, line in enumerate(f):
     line_a = i
     lower_bound = i + 1
     upper_bound = i + 4
     with open('C:/working_file.txt', mode='r', encoding = 'utf8') as g:
        for j, line in enumerate(g):
            while j >= lower_bound and j <= upper_bound:
                line_b = j
                j = j+1
                print(line_a, line_b)

Instead of the output I want as above, it gives me the following:

990     991
990     992
990     993
990     994
990     992
990     993
990     994
990     993
990     994
990     994

As you can see, the inner loop repeats several times for each row in the outer loop. It looks like there should be only one iteration per line in the outer loop. What am I missing?

EDIT: My question was given below, here is the exact code I ended up using:

from collections import deque
from itertools import cycle
log = open('C:/example.txt', mode='w', encoding = 'utf8') 
try:
    xrange 
except NameError: # python3
    xrange = range

def pack(d):
    tup = tuple(d)
    return zip(cycle(tup[0:1]), tup[1:])

def window(seq, n=2):
    it = iter(seq)
    d = deque((next(it, None) for _ in range(n)), maxlen=n)
    yield pack(d)
    for e in it:
        d.append(e)
        yield pack(d)

for l in window(open('c:/working_file.txt', mode='r', encoding='utf8'),100):
    for a, b in l:
        print(a.strip() + '\t' + b.strip(), file=log)
+4
5

- :

from collections import deque
from itertools import cycle

try:
    xrange 
except NameError: # python3
    xrange = range

def pack(d):
    tup = tuple(d)
    return zip(cycle(tup[0:1]), tup[1:])

def window(seq, n=2):
    it = iter(seq)
    d = deque((next(it, None) for _ in xrange(n)), maxlen=n)
    yield pack(d)
    for e in it:
        d.append(e)
        yield pack(d)

:

>>> for l in window([1,2,3,4,5], 4):
...     for l1, l2 in l:
...         print l1, l2
...
1 2
1 3
1 4
2 3
2 4
2 5

, , :

window(open('C:/working_file.txt', mode='r', encoding='utf8'), 4)
+5

. , :

with open('C:/working_file.txt', mode='r', encoding = 'utf8') as f: 
    data = f.readlines()

for i, line_a in enumerate(data):
    for j, line_b in enumerate(data[i+1:i+5], start=i+1):
        print(i, j)

, enumerate for line_b in data[i+1:i+5]. , , , , .

+1

, - ...

my_data = {}
for i, line in enumerate(f):
    my_data[i] = line

for x in my_data:
    for y in range(1, 4):
        print my_data[x], my_data[x + y]

, ...

0

, . , , , .

  • N , N - .

    • , .
    • .
    • ..
  • N, . , N .

, , . , max, N! .

0

Based on alko's answer, I would suggest using an windowunmodified recipe

from itertools import islice

def window(seq, n=2):
    "Returns a sliding window (of width n) over data from the iterable"
    "   s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ...                   "
    it = iter(seq)
    result = tuple(islice(it, n))
    if len(result) == n:
        yield result    
    for elem in it:
        result = result[1:] + (elem,)
        yield result

for l in window([1,2,3,4,5], 4):
    for item in l[1:]:
        print l[0], item
0
source

All Articles