简体   繁体   中英

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?

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

.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('([()])'))

Try this:

def find_married_braces(s):
    for i in range(len(s)-1):
        a, b = s[i], s[i+1]
        if (a == '(' and b == ')') or (a == '[' and b == ']') or (a == '{' and b == '}'):
            return i, i+1
    return False


print(find_married_braces('{{]]'))
print(find_married_braces('{{}}'))
print(find_married_braces('([()])'))
print(find_married_braces('([{}])'))

Output

False
(1, 2)
(2, 3)
(2, 3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM