a splitting headache
- From: Mensanator <mensanator@xxxxxxx>
- Date: Thu, 15 Oct 2009 18:18:09 -0700 (PDT)
All I wanted to do is split a binary number into two lists,
a list of blocks of consecutive ones and another list of
blocks of consecutive zeroes.
But no, you can't do that.
['', '', '1', '', '', '', '11', '']c = '0010000110'
c.split('0')
Ok, the consecutive delimiters appear as empty strings for
reasons unknown (except for the first one). Except when they
start or end the string in which case the first one is included.
Maybe there's a reason for this inconsistent behaviour but you
won't find it in the documentation.
And the re module doesn't help.
['', '', '1', '2', '', '3', '', '', '4', '', '', '', '']f = ' 1 2 3 4 '
re.split(' ',f)
OTOH, if my digits were seperated by whitespace, I could use
str.split(), which behaves differently (but not re.split()
because it requires a string argument).
['1', '11', '111', '11']' 1 11 111 11 '.split()
That means I can use re to solve my problem after all.
['1', '11']c = '0010000110'
re.sub('0',' ',c).split()
['00', '0000', '0']re.sub('1',' ',c).split()
Would it have been that difficult to show in the documentation
how to do this?
.
- Follow-Ups:
- Re: a splitting headache
- From: jhermann
- Re: a splitting headache
- From: David C Ullrich
- Re: a splitting headache
- From: Thomas
- Re: a splitting headache
- From: Paul Rubin
- Re: a splitting headache
- From: John O'Hagan
- Re: a splitting headache
- From: Ishwor Gurung
- Re: a splitting headache
- From: Mel
- Re: a splitting headache
- Prev by Date: Raw_input with readline in a daemon thread makes terminal text disappear
- Next by Date: How about adding slice notation to iterators/generators?
- Previous by thread: Raw_input with readline in a daemon thread makes terminal text disappear
- Next by thread: Re: a splitting headache
- Index(es):
Relevant Pages
|