0

I have a list:

temp_token = ['(', '(', '(', '(', 4.0, '+', 3.0, ')', ')', ')', '+', 7.0, ')', '+', '(', 2.0, '+', 9.0, ')']

and I need to find the index of the last "(" before ")" is met.

for n in range(0,len(temp_token)):
    if temp_token[n] == ")":
        number = n
        break
index = len(temp_token) - 1 - temp_token[number::-1].index("(")    
print(index)

index should be 3 instead of 14

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Tommy2806
  • 43
  • 4
  • 1
    iterate in reverse order, first "(" is the answer – prashant May 17 '19 at 03:22
  • 1
    in his example, he has an area enclosed in a separate set of parenthesis, after some pairs already open and close. iterating reversely and returning the first "(" would return the one "'(', 2.0, '+', 9.0, ')'" there, while index 3 would be at the beginning. – azb_ May 17 '19 at 03:25

5 Answers5

0

Try something like this:

def find_brackets(arr):
    cur_index = 0
    for i in range(0, len(arr)):
        if arr[i] == "(":
            cur_index=i
        elif arr[i] == ")":
            return cur_index

It keeps track of the latest "(", and returns as soon as a ")" is found.

azb_
  • 387
  • 2
  • 10
0
temp_token = ['(', '(', '(', '(', 4.0, '+', 3.0, ')', ')', ')', '+', 7.0, ')', '+', '(', 2.0, '+', 9.0, ')']
last_index = 0
for k,v in enumerate(temp_token):
    if v == '(':
        last_index = k
    elif v == ')':
        break
print(last_index)

OUTPUT

3
Nithin
  • 1,065
  • 8
  • 15
0

Your code doesn't work because you do len(temp_token) - 1 when you should be doing len(temp_token[:number]) - 1 or just number - 1. You also don't need the first for loop, you can just do number = temp_token.index(")"). All together it would look like:

number = temp_token.index(")")

index = len(temp_token[:number])-1 - temp_token[number::-1].index("(")
# or
index = number-1 - temp_token[number::-1].index("(")

but I think a function with a for loop would be easier to read

def find_last_left_bracket_before_first_right_bracket(lyst):
    left_bracket_index = -1
    for index, element in enumerate(lyst):
        if element == "(":
            left_bracket_index = index
        if element == ")":
            return left_bracket_index
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
0

Another approach without for loop is to use str.join with str.rindex:

temp_token = ['(', '(', '(', '(', 4.0, '+', 3.0, ')', ')', ')', '+', 7.0, ')', '+', '(', 2.0, '+', 9.0, ')']
s = ''.join(map(str,temp_token))
s[:s.index(')')].rindex('(')

Output:

3
Chris
  • 29,127
  • 3
  • 28
  • 51
-1

I guess you want to do it multiple times. So use the stack.

temp_token = ['(', '(', '(', '(', 4.0, '+', 3.0, ')', ')', ')', '+', 7.0, ')', '+', '(', 2.0, '+', 9.0, ')']

stack = []
for n in range(0,len(temp_token)):
    if temp_token[n] == "(":
        stack.append(n)
    if temp_token[n] == ")":
        print(stack.pop())
        # break if just do it once
HolaYang
  • 419
  • 2
  • 10
  • 1
    I didn't downvote, but first off you should [very rarely have to use `range(len(a))`](https://stackoverflow.com/questions/19184335/is-there-a-need-for-rangelena), in this case `enumerate(temp_token)` would be more pythonic. And there's no reason to use a stack, you can just have a variable. – Boris Verkhovskiy May 17 '19 at 03:59