I want to be able to turn a user input string into a string list and then modify that list as needed.For example sake I will be useing: (a+b)(b+a). where the final result is list[0]=a+b and list[1]=b+a.
so far i have figured out how to use the re.split tool to break my example string into a string list.
list[0]=
list[1]=a+b
list[2]=b+a
list[3]=
I would like to remove the blank strings from my list.
currently I have tried using filter(None,…):shown below
import re
original = "(a+b)(b+a)"
sections = list()
sections=filter(None,re.split(r"[()]+",original))
print(sections[0])
this has given me: “TypeError: 'filter' object is not subscriptable
” while I understand the words im not entirely sure what this means.
I tried to pull the filter out:
import re
original = "(a+b)(b+a)"
sections = list()
sections=re.split(r"[()]+",original)
print(sections)
sections=filter(None, sections)
print(sections)
And now I get “<filter object at 0x105020df0>” which makes even less sense to me then the words above. Please help me remove my blank string . Thanks in advance.
George Volz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Actually, re.findall
with the regex pattern ((.*?))
is a better tool for the job:
import re
original = "(a+b)(b+a)"
parts = re.findall(r'((.*?))', original)
print(parts) # ['a+b', 'b+a']
Note: This answer assumes that the (...)
tuples in the input string are not nested. If there are nested parentheses, then the above approach won’t work (but neither would re.split
). In that case, we would need to use or write a parser.
In Python 3, filter
returns an iterator, not a list. You need to explicitly convert it to a list if you want to access elements by index.
The TypeError
is caused because sections is an iterator, not a list.
Here’s the corrected version of the code:
import re
original = "(a+b)(b+a)"
sections = list(filter(None, re.split(r"[()]+", original)))
print(sections[0])
This will output: a+b
This code correctly splits the string on parentheses and filters out any empty strings, then prints the first non-empty section.