You could try something like the following code I wrote:
Explanation:
If behind
/forward
is 0 we set variable behind_counter
/forward_counter
to 1 in order for the following loops to atleast loop once.
Outer loop loops over the range of seq
, the two inner loops over range of behind_counter
(counted down) and forward_counter
(counted up),respectively. Inside the most inner loop we set the respective lookahead/-behind indices and then check with help of the three if
statements whether indices are out of bound in order to set the respective values to the out of bound value ('null'
) if necessary. The fourth if
statement is chosen if the indices are not out of bound. Inside each if
statement there are if-elif-elif-else
statements which change the appended tuple depending on the lookahead/-behind values stored in behind
and forward
. If both are 0 only append seq[i]
, if behind
is 0 only append a tuple consisting of seq[i]
and current lookahead value, and so on.
After the work is done we print the value of res
in order to visualize the result.
Source Code:
seq = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
behind = 0
forward = 3
res = []
behind_counter = behind
forward_counter = forward
if behind == 0:
behind_counter = 1
if forward == 0:
forward_counter = 1
for i in range(len(seq)):
for j in range(behind_counter,0,-1):
for k in range(forward_counter):
index_behind = i - j
index_forward = i + k + 1
if index_behind < 0 and index_forward > len(seq):
index_behind = 'null'
index_forward = 'null'
if behind == 0 and forward == 0:
res.append(tuple((seq[i])))
elif behind == 0:
res.append(tuple((seq[i],index_forward)))
elif forward == 0:
res.append(tuple((index_behind,seq[i])))
else:
res.append(tuple((index_behind,seq[i],index_forward)))
continue
if index_behind < 0:
index_behind = 'null'
if behind == 0 and forward == 0:
res.append(tuple((seq[i])))
elif behind == 0:
res.append(tuple((seq[i],seq[index_forward])))
elif forward == 0:
res.append(tuple((index_behind,seq[i])))
else:
res.append(tuple((index_behind,seq[i],seq[index_forward])))
continue
if index_forward >= len(seq):
index_forward = 'null'
if behind == 0 and forward == 0:
res.append(tuple((seq[i])))
elif behind == 0:
res.append(tuple((seq[i],index_forward)))
elif forward == 0:
res.append(tuple((seq[index_behind],seq[i])))
else:
res.append(tuple((seq[index_behind],seq[i],index_forward)))
continue
if index_forward < len(seq) and index_behind >= 0:
if behind == 0 and forward == 0:
res.append(tuple((seq[i])))
elif behind == 0:
res.append(tuple((seq[i],seq[index_forward])))
elif forward == 0:
res.append(tuple((seq[index_behind],seq[i])))
else:
res.append(tuple((seq[index_behind],seq[i],seq[index_forward])))
print (res)
Output:
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (3, 6), (4, 5), (4, 6), (4, 7), (5, 6), (5, 7), (5, 8), (6, 7), (6, 8), (6, 9), (7, 8), (7, 9), (7, 10), (8, 9), (8, 10), (8, 'null'), (9, 10), (9, 'null'), (9, 'null'), (10, 'null'), (10, 'null'), (10, 'null')]
First Major Update:
According to a new comment you want a different ouput when both lookbehind/-forward are specified, so I altered my program to now hopefully fulfill your needs:
Additional explanation for updated program:
In each iteration of the outer loop first the lookbehind pairs are added in a loop, then the lookforward pairs are added, also in a loop. As before we check for out of bounds and set the value of the lookbehind/forward value accordingly.
Updated source code:
seq = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
behind = 2
forward = 3
res = []
behind_counter = behind
forward_counter = forward
if behind == 0:
behind_counter = 1
if forward == 0:
forward_counter = 1
for i in range(len(seq)):
for j in range(behind_counter,0,-1):
index_behind = i - j
if behind == 0:
#res.append(tuple((seq[i])))
continue
else:
if index_behind < 0:
index_behind = 'null'
res.append(tuple((seq[i],index_behind)))
continue
else:
res.append(tuple((seq[i], seq[index_behind])))
for k in range(forward_counter):
index_forward = i + k + 1
if forward == 0:
#res.append(tuple((seq[i])))
continue
else:
if index_forward >= len(seq):
index_forward = 'null'
res.append(tuple((seq[i],index_forward)))
continue
else:
res.append(tuple((seq[i],seq[index_forward])))
print (res)
New output: [when lookforward and lookbehind are specified]
[(1, 'null'), (1, 'null'), (1, 2), (1, 3), (1, 4), (2, 'null'), (2, 1), (2, 3), (2, 4), (2, 5), (3, 1), (3, 2), (3, 4), (3, 5), (3, 6), (4, 2), (4, 3), (4, 5), (4, 6), (4, 7), (5, 3), (5, 4), (5, 6), (5, 7), (5, 8), (6, 4), (6, 5), (6, 7), (6, 8), (6, 9), (7, 5), (7, 6), (7, 8), (7, 9), (7, 10), (8, 6), (8, 7), (8, 9), (8, 10), (8, 'null'), (9, 7), (9, 8), (9, 10), (9, 'null'), (9, 'null'), (10, 8), (10, 9), (10, 'null'), (10, 'null'), (10, 'null')]
Second Major Update:
In case you want a list containing tuples of tuples as a result you could do something like this [I slightly modified the code of my first major update]:
Additional explanation:
In the beginning of each outer loop iteration we append an empty list to res
. To this list we append the according values of first the lookbehind pairs, then the lookforward pairs. In the end of each outer loop iteration we then convert this newly created list to a tuple.
seq = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
behind = 2
forward = 3
res = []
behind_counter = behind
forward_counter = forward
if behind == 0:
behind_counter = 1
if forward == 0:
forward_counter = 1
for i in range(len(seq)):
res.append(list())
for j in range(behind_counter,0,-1):
index_behind = i - j
if behind == 0:
#res.append(tuple((seq[i])))
continue
else:
if index_behind < 0:
index_behind = 'null'
res[i].append((seq[i],index_behind))
continue
else:
res[i].append((seq[i], seq[index_behind]))
for k in range(forward_counter):
index_forward = i + k + 1
if forward == 0:
#res.append(tuple((seq[i])))
continue
else:
if index_forward >= len(seq):
index_forward = 'null'
res[i].append((seq[i],index_forward))
continue
else:
res[i].append((seq[i],seq[index_forward]))
res[i] = tuple(res[i])
print (res)
Output: [list containing tuples of tuples]
[((1, 'null'), (1, 'null'), (1, 2), (1, 3), (1, 4)), ((2, 'null'), (2, 1), (2, 3), (2, 4), (2, 5)), ((3, 1), (3, 2), (3, 4), (3, 5), (3, 6)), ((4, 2), (4, 3), (4, 5), (4, 6), (4, 7)), ((5, 3), (5, 4), (5, 6), (5, 7), (5, 8)), ((6, 4), (6, 5), (6, 7), (6, 8), (6, 9)), ((7, 5), (7, 6), (7, 8), (7, 9), (7, 10)), ((8, 6), (8, 7), (8, 9), (8, 10), (8, 'null')), ((9, 7), (9, 8), (9, 10), (9, 'null'), (9, 'null')), ((10, 8), (10, 9), (10, 'null'), (10, 'null'), (10, 'null'))]