1

Another way to say this is "find all substrings within a string"; while there's a bunch of these posts on stackoverflow already, none I've found quite get the job done.

#query variable
query = "select * from table1 a join table2 b on a.id = b.id left join table3 c on b.id = c.id where x = 5;"
query_list = query.split()

#find the adjacent word in a list
def find_adjacents(some_value, some_list):
    i = some_list.index(some_value)
    return some_list[i:i+2]

print('from tables:', find_adjacents("from", query_list)[1])
print('join tables:', find_adjacents("join", query_list)[1])

>>>from tables: table1
>>>join tables: table2

There should be one more join tables: table3. How do I get this to print All instances of the adjacent substring "join"?

1 Answer 1

3

Is this what you're after?

# Query variable
query = "select * from table1 a join table2 b on a.id = b.id left join table3 c on b.id = c.id where x = 5;"
query_list = query.split()

def find_adjacents(some_value, some_list):
    val = [index for index, value in enumerate(some_list) if value == some_value]

    allVal = []
    for x in val:
        allVal.append(some_list[x+1])
    return allVal

>>> print('from tables:', find_adjacents("from", query_list))
from tables: ['table1']

>>> print('join tables:', find_adjacents("join", query_list))
join tables: ['table2', 'table3']
Sign up to request clarification or add additional context in comments.

1 Comment

Yes sir brother (or woman!) - that's exactly what i was looking for. thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.