I have written some code to search a for a pattern in a list of tuples.
It works, but I am sure that it can be improved. Any feedback or ideas would be greatly appreciated.
The list of tuples I am searching in looks like this:
x = [('A', True), ('B', True), ('C', False), ('D', False), ('X', False), ('E', True), ('F', True), ('G', True)]
I want to find X and then also grab all the values in the tuple adjacent to X that have a value of true.
So the expected output would be:
X E F G
For:
y = [('X', True), ('1', True), ('2', True), ('3', True), ('4', False), ('5', True)]
The expected output would be:
X 1 2 3
For:
z = [('Dog', True), ('Cow', False), ('Shark', True), ('Cat', True), ('X', False)]
The expected output would be:
Shark Cat X
For:
z = [('C', True), ('X', False), ('D', True), ('C', False), ('S', True)]
The expected output would be:
C X D
This is my code:
test = [('A', False), ('B', False), ('C', True), ('D', True),
('E', True), ('X', False), ('G', True), ('H', False)]
def find_x(x):
for item in x:
if item[0] == 'X':
index = x.index(item)
return(index)
return(None)
def look_around(x, index):
left = 0
right = len(x)-1
found_left = False
found_right = False
#look left:
if index != 0:
for i in range(index, -1, -1):
if x[i][1] == True:
left = i
found_left = True
if x[i][1] == False and i != index:
break
#look right
if index != len(x)-1:
for i in range(index, len(x), +1):
if x[i][1] == True:
right = i
found_right = True
if x[i][1] == False and i != index:
break
if found_left and found_right:
return(left, right)
elif found_left and not found_right:
return(left, index)
elif not found_left and found_right:
return(index, right)
else:
return (index, index)
index_of_x = find_x(test)
if index_of_x != None:
left, right = look_around(test, index_of_x)
print "match:"
for i in range (left, right+1, +1):
print test[i][0], #comma prints everything on same line