Given a list of elements, how to create a window of an arbitrary size, and extract n elements to the left and right from the center? For example, given:
l = ['This', 'document', 'is', 'the', 'second', 'document']
and a window = 2
How to extract the following sublist:
['document', 'is', 'the', 'second', 'document']
So far I tried to:
middle = int((len(l)/2))
middle_element = l[middle]
middle_index = l.index(l[middle])
left_side = l[:middle_index]
right_side = l[middle_index:]
sublist = left_side right_side
The problem with my approach is that it doesnt work for any list of any size. How should I control sliding from the left and right once I find the center of the list?
CodePudding user response:
Here is a way to do what your question asks:
l = ['This', 'document', 'is', 'the', 'second', 'document']
window = 2
middle = int((len(l)/2))
sublist = l[max(0, middle - window):min(len(l), middle window 1)]
Output:
['document', 'is', 'the', 'second', 'document']
Explanation:
We take a slice of l
starting at middle - window
(but no less than 0 as enforced using max()
). Ignoring edge cases, this ensures we get middle - window
, middle - window 1
, ..., middle - window window
which are a total of window 1
list items (numbering window
items to the left of middle
in addition to the item at index middle
). Our slice ends at middle window 1
(guarded against going out of range by min()
, though technically this is not required as slices are forgiving of end-of-range values greater than the sequence length) which, in addition to the items already accounted for above, adds the items middle 1
, ..., middle window
(but not the item middle window 1
as slice notation does not include the end of the slice range) which count for an additional window
items to the right of the the middle
index.