[Tutor] Python "returning .split() without empty strings

An Liu sourceonly at gmail.com
Sat Oct 24 07:10:12 EDT 2020


Hi

you might have a look at re.spilt
import re
res=re.spilt(r”0+”, string)

I didn’t test it myself, but should not far away from
Working

FYI




On Sat, Oct 24, 2020 at 13:03 Chris C <mysecretrobotfactory at gmail.com>
wrote:

> Hi all, I have a question regarding string operations.
> the following string needs to be chopped up, having the 0's dividing the
> data that I wanted, therefore the string here becomes
>
> # run this
> string = '1111022220000333300044405550055'
> string = string.split('0')
>
> # get this
> ['1111', '2222', '', '', '', '3333', '', '', '444', '555', '', '55']
>
> # but I want it without the empty strings, so I ran another loop to pick up
> # only what I wanted
>
> key_list = list()
>
> for key in string :
> if key != '':
> key_list.append(key)
>
> print(key_list)
>
>
> # the above is this
> ['1111', '2222', '3333', '444', '555', '55']
>
>
> The point is to remove all the 0's, however many there are.
> I hope I have explained it well, please let me know if I didn't.
>
> So, the question is, is it possible to do this without the 2nd stage?
>
> Thanks!
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
-- 
Liu An


More information about the Tutor mailing list