My first idea is to use slice [i-offset:i+offset+1]
If lists have different lengths then you can get shorter length
shorter = min(len(detection), len(ground_truth))
To works with lists separatelly you have to first find indexes.
I use [offset:shorter-offset]
because I assumed that you don't want to check if there is not enought elements on left or right (if there are less elements then offset
).
indexes = [i for i, val in enumerate(detection[offset:shorter-offset], offset) if val == 1]
And now you can use indexes
for i in indexes:
#item = detection[i-offset:i] + detection[i+1:i+1+offset]
# or
item = detection[i-offset:i+offset+1]
item.pop(offset) # remove value in the middle
print(' detection item:', item)
I don't know what you try to do with or
logic - so I skip it.
Code - with offset=2
detection = [0,0,1,0,1,1,0,1,0,1,1] # longer
ground_truth = [0,1,0,0,0,0,1,0]
#detection = [0,0,1,0,0,0,1,0,0] # shorter
#ground_truth = [0,0,1,0,1,1,0,1,0,1,1]
print(' detection:', detection)
print('ground_truth:', ground_truth)
offset = 2
shorter = min(len(detection), len(ground_truth))
indexes = [i for i, val in enumerate(detection[offset:shorter-offset], offset) if val == 1]
print('indexes:', indexes)
for i in indexes:
#item = detection[i-offset:i] + detection[i+1:i+1+offset]
# or
item = detection[i-offset:i+offset+1]
item.pop(offset) # remove value in the middle
print(' detection item:', item)
for i in indexes:
#item = ground_truth[i-offset:i] + ground_truth[i+1:i+1+offset]
# or
item = ground_truth[i-offset:i+offset+1]
item.pop(offset) # remove value in the middle
print('ground_truth item:', item)
Result:
detection: [0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1]
ground_truth: [0, 1, 0, 0, 0, 0, 1, 0]
indexes: [2, 4, 5]
detection item: [0, 0, 0, 1]
detection item: [1, 0, 1, 0]
detection item: [0, 1, 0, 1]
ground_truth item: [0, 1, 0, 0]
ground_truth item: [0, 0, 0, 1]
ground_truth item: [0, 0, 1, 0]
Second idea is to use shift()
to move value from previous/next row to the same row but to new column. But with new information I think it creates too many new columns so I removed it.
I was wondering if it could be done with rolling(window=3)
but I couldn't create solution.
Doc: shift, apply, rolling