数据分析2
作者:互联网
""" 输入参数: line(first-numberl (last-numberl)l(aux|vty) first-number2 (last-number2)) 输出参数: (first-numberl (last-number1)l(aux|vty) first-number2 (last-number2)) (last-numberl) (aux|vty) (last-number2) 注释:取出括号中的元素(涉及到多级括号) I """ def get_indexes(a, b): """ 找到元素出现的所有索引 :param a: 被寻找元素 :param b: 内容 :return: """ return [n for n in range(len(b)) if b.find(a, n) == n] def output(p): """ 左侧跟右侧哪个括号可以合成一组? 获取左、右边括号的所有索引 符合条件 1.中间没有括号必取 2.左侧括弧与右侧括弧间,左括弧数等于右括弧数 :param : :return: """ result = [] left_n = get_indexes("(", p) right_n = get_indexes(")", p) for l in left_n: for r in right_n: if r > l: c_p = p[l:r + 1] l_count = c_p.count("(") r_count = c_p.count(")") if l_count == r_count: result.append(c_p) break return result txt = """ line(first-numberl (last-numberl)l(aux|vty) first-number2 (last-number2)) """ print(output(txt)) # [ # '(first-numberl (last-numberl)l(aux|vty) first-number2 (last-number2))', # '(last-numberl)', # '(aux|vty)', # '(last-number2)' # ]
标签:数据分析,last,numberl,number2,vty,aux,first 来源: https://www.cnblogs.com/zhangguosheng1121/p/16168334.html