Python program to check matching of simple parentheses

后端 未结 25 2411
谎友^
谎友^ 2020-12-01 17:02

I am a Python newbie and I came across this exercise of checking whether or not the simple brackets \"(\", \")\" in a given string are matched evenly.

I have seen ex

25条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-01 17:36

    More advanced example in which you additionally need to check a matching of square brackets '[]' and braces '{}' pars.

    string = '([]{})'
    
    def group_match(string):
    
        d = {
        ')':'(',
        ']':'[',
        '}':'{'
        }
    
        list_ = []
    
        for index, item in enumerate(string):
            if item in d.values():
                list_.append(item)
    
            elif (item in d.keys()) and (d.get(item) in list_):
                list_.pop()
    
        return len(list_) == 0
    

提交回复
热议问题