Is it possible to take out text in pairs of brackets out of a string into a list, with the non-bracketed text in another list, with both lists being nested in the same list? This is what I mean:
"hello{ok}why{uhh}so"
--> [["hello","why","so"],["ok","uhh"]]
CodePudding user response:
As per the sample you shared, this is quite easy with re
modules. However, if your text is huge, you will have to think about improvising this solution. Using re, you can do something like below
import re
raw_text = "hello{ok}why{uhh}so"
result = [re.split(r"{[A-Za-z]*}", raw_text),re.findall(r"{([A-Za-z]*)}",raw_text)]
print(result)
produces a result
[['hello', 'why', 'so'], ['ok', 'uhh']]
CodePudding user response:
Following piece of code may help you
input_str = "hello{ok}why{uhh}so"
list1, parsed_parentheses = [], []
for index in range(len(input_str)):
if input_str[index] == "{":
parsed_parentheses.append(input_str[index])
substr = ""
continue
else:
if parsed_parentheses == []:
continue
if input_str[index] == "}":
parsed_parentheses.append(input_str[index])
list1.append(substr)
if "{" == parsed_parentheses[-1]:
substr = input_str[index]
input_str = input_str.replace("{", "-").replace('}', "-").split('-')
list2 = list(set(input_str) - set(list1))
result = [list2, list1]
It produces the following result
[['hello', 'why', 'so'], ['ok', 'uhh']]