def get_elements(s): q = [] while len(s) != 1: q.append(s[0]) s = s[1] q.append(s)
return q
def nested_list(l): res = () ele0 = l[0] res += (ele0, ) l.remove(ele0) if len(l) > 0: res += ((nested_list(l), ))
return res
def main(): s = ('a', ('b', ('c', ('d', ('e', ('f')))))) l = get_elements(s) l = l[::-1] ll = nested_list(l) print(ll)
if __name__ == '__main__': main()
xiongshengyao
2018-03-26 15:42:41 +08:00
最简单的思路…一个队列一个栈 ``` def main(source_str): el = [] ret = [] for i in source_str: if i not in ('(', ',', ')'): el.append(i) ret.append("#") else: ret.append(i)
for index, value in enumerate(ret): if value == "#": ret[index] = el.pop()
return "".join(ret)
if __name__ == "__main__": result = main("(a,(b,(c,(d,(e,(f))))))") print(result) ```