+ 2

Why am I getting this output ?

I have written the following code for splitting a string whenever it encounters with ":-, -, :" Program : ---------------- import re a="This:-Coding:in:-python-solo-learn" b=re.split(r"(:-)|:|-",a) print(b) OUTPUT -------------- ['This', ':-', 'Coding', None, 'in', ':-', 'python', None, 'solo', None, 'learn'] But instead I need to get the following output i.e., ['This', ':-', 'Coding', ':', 'in', ':-', 'python', '-', 'solo', '-', 'learn'] Where did the code went wrong, and can anyone please give explanation for the kind of output I got ? Also how can I correct it inorder to obtain desired output ? https://code.sololearn.com/cwKz8orIamN8/?ref=app

25th Apr 2020, 5:14 AM
Kiran Deep Naidu
Kiran Deep Naidu - avatar
7 Réponses
+ 4
Your b variable should be like this. b=re.split(r"(:-|:|-)", a)
25th Apr 2020, 6:14 AM
Maninder $ingh
Maninder $ingh - avatar
+ 3
Fixed: b=re.split(r":-|:|-",a) This way you get only the words, all the separator marks are skipped. In regex, parentheses mean a captured group of characters. I did not actually find this documented in the python spec, but I think parentheses force an extra element in the result list, but it was replaced by None because how split works. It's a bit hard to explain, but somehow makes sense to me.
25th Apr 2020, 6:25 AM
Tibor Santa
Tibor Santa - avatar
+ 2
I'm not really good at regex, there is probably a better way to do this but until then this will work. b=re.split(r"(:-)|(\:)|(\-)",a) b=[x for x in b if x != None]
25th Apr 2020, 6:03 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Maninder $ingh is correct and the better way.
25th Apr 2020, 6:16 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Try this and hopefully it clears up what I meant: import re a="split with whitespace" b=re.split("( )",a) print(b)
25th Apr 2020, 6:29 AM
Tibor Santa
Tibor Santa - avatar
+ 1
ChaoticDawg I got the point which you are conveying and thanks because this helps me alot. But why does your program also raises "None" when it is split? Please let me know if you have an answer for that
25th Apr 2020, 6:14 AM
Kiran Deep Naidu
Kiran Deep Naidu - avatar
+ 1
Kiran Deep Naidu I'm not really sure why it throws the extra None values into the resulting list. I don't use regex enough to have a sufficient answer. I was just googling it when Maninder $ingh gave the correct way. I was thinking (:-)[:-] would have been correct but now after seeing the correct answer, that makes more sense.
25th Apr 2020, 6:21 AM
ChaoticDawg
ChaoticDawg - avatar