Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected output of string function in Python 3

I'm writing code in Python 3 - or trying to - that takes in a string of braces such as '{{]]' and looks for an adjacent pair of braces which is either '[' followed immediately by ']', or '{' followed immediately by '}', or '(' followed immediately by ')'. It returns the first pair it finds, or False if it doesn't find any. To that end, I wrote some code:

# write a function that scans a sequence of braces to find a "married pair", or else returns false if there is none.  
def find(mystring):
    for x in mystring[0:-1]:
        y=mystring[mystring.index(x)+1:][0]
        if (x == '[' and y == ']') or (x == '{' and y == '}') or (x == '(' and y == ')'):
            return x,y
        else:
            continue
    return False


So OK it works when I try the find function on the string ({}). It returns ('{','}') as expected. But when I try find ('{{}}') it unexpectedly returns False. Running ([()]) unexpectedly returns False. ([{}]) returns True as expected.

What is going on here?

like image 757
John Smith Avatar asked Feb 17 '26 04:02

John Smith


2 Answers

mystring.index(x) doesn't return the index you're looking at. It returns the first index of the given character. So when you're at the second { character of {{}}, index(x) returns 0. If you're using indexes while iterating, use enumerate.

A fixed version could be like this:

def find(mystring):
    for i,x in enumerate(mystring[:-1]):
        y = mystring[i+1]
        if (x == '[' and y == ']') or (x == '{' and y == '}') or (x == '(' and y == ')'):
            return x,y
    return False
like image 177
khelwood Avatar answered Feb 18 '26 20:02

khelwood


.index() returns the FIRST index of the occurrence of that char, so when you have more then 1, you get the wrong y.

you can pair adjacent chars using zip like this:

def find(mystring):
    pairs = zip(mystring, mystring[1:])
    for x, y in pairs:
        if (x == '[' and y == ']') or (x == '{' and y == '}') or (x == '(' and y == ')'):
            return x, y
        else:
            continue
    return False


print(find('({})'))
print(find('{{}}'))
print(find('([()])'))
like image 35
Adam.Er8 Avatar answered Feb 18 '26 21:02

Adam.Er8