1

Writing '1000011'.split('1') gives

['', '0000', '', '']

What I want is:

['1', '0000', '11']

How do I achieve this?

1
  • It's unclear if you want just split up a bunch of 1 and 0 characters or if there will be anything else in there besides 1 and 0. Commented Oct 10, 2015 at 16:25

2 Answers 2

2

The str.split(sep) method does not add the sep delimiter to the output list.

You want to group string characters e.g. using itertools.groupby:

In:  import itertools

In:  [''.join(g) for _, g in itertools.groupby('1000011')]
Out: ['1', '0000', '11']

We didn't specify the key argument and the default key function just returns the element unchanged. g is then the group of key characters.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use regex :

>>> re.findall(r'0+|1+','1000011')
['1', '0000', '11']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.