1

I am trying to use split function without empty string. For instance, from a string

['#1(X,Y)', '#2(X,Z)', '#3(Z,Y)']

I want to get:

[ ['#1', 'X', 'Y'],['#2', 'X', 'Z'],['#3', 'Z', 'Y']]

I tried

[ re.split('[(),]', item) for item in string ] 

but I get

[ ['#1', 'X', 'Y', ''],['#2', 'X', 'Z', ''],['#3', 'Z', 'Y', '']]

How can I remove ''?

Thanks

0

4 Answers 4

1

It's ugly but you could remove the last element of each sub-list (assuming they are always present and always at the end):

[re.split('[(),]', item)[:-1] for item in string] 
Sign up to request clarification or add additional context in comments.

Comments

1

You can filter out the empty characters inside the comprehension:

[ [each for each in re.split('[(),]', item) if each != ''] for item in string ] 

Out[77]: [['#1', 'X', 'Y'], ['#2', 'X', 'Z'], ['#3', 'Z', 'Y']]

If you are sure empty string is always the last element, you can just discard the last element using the solution provided by @jtlz2

[ re.split('[(),]', item)[:-1] for item in string ] 

Out[78]: [['#1', 'X', 'Y'], ['#2', 'X', 'Z'], ['#3', 'Z', 'Y']]

Comments

0

Another option is filter bool:

import re

string = ['#1(X,Y)', '#2(X,Z)', '#3(Z,Y)']

print([list(filter(bool, re.split('[(),]', item))) for item in string])
[['#1', 'X', 'Y'], ['#2', 'X', 'Z'], ['#3', 'Z', 'Y']]

Comments

0

You could strip the ) before splitting.

[re.split('[(,]', s.strip(')')) for s in l]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.