Re: str.split() with empty separator

2009-09-17 Thread David C Ullrich
On Tue, 15 Sep 2009 14:31:26 +0200, Ulrich Eckhardt wrote:

 Hi!
 
 'abc'.split('') gives me a ValueError: empty separator. However,
 ''.join(['a', 'b', 'c']) gives me 'abc'.
 
 Why this asymmetry?

The docs say 

If sep is given, consecutive delimiters are not grouped together and are 
deemed to delimit empty strings (for example, '1,,2'.split(',') returns 
['1', '', '2']). 

Now suppose sep = ''. That means split() should return an infinitely
long list of empty strings! Because if sep = '' then the
string 'hello' starts with an empty string followed by sep
followed by an empty string followed by sep followed by an
empty string followed by sep... that's all before we get to
the 'h'.


 I was under the impression that the two would be
 complementary.
 
 Uli

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: str.split() with empty separator

2009-09-16 Thread Hendrik van Rooyen
On Tuesday 15 September 2009 14:50:11 Xavier Ho wrote:
 On Tue, Sep 15, 2009 at 10:31 PM, Ulrich Eckhardt

 eckha...@satorlaser.comwrote:
  'abc'.split('') gives me a ValueError: empty separator.
  However, ''.join(['a', 'b', 'c']) gives me 'abc'.
 
  Why this asymmetry? I was under the impression that the two would be
  complementary.

 I'm not sure about asymmetry, but how would you implement a split method
 with an empty delimiter to begin with? It doesn't make much sense anyway.

I fell into this trap some time ago too.
There is no such string method.

The opposite of  .join(aListOfChars) is
list(aString)

- Hendrik

-- 
http://mail.python.org/mailman/listinfo/python-list


str.split() with empty separator

2009-09-15 Thread Ulrich Eckhardt
Hi!

'abc'.split('') gives me a ValueError: empty separator.
However, ''.join(['a', 'b', 'c']) gives me 'abc'.

Why this asymmetry? I was under the impression that the two would be
complementary.

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: str.split() with empty separator

2009-09-15 Thread Xavier Ho
On Tue, Sep 15, 2009 at 10:31 PM, Ulrich Eckhardt
eckha...@satorlaser.comwrote:

 'abc'.split('') gives me a ValueError: empty separator.
 However, ''.join(['a', 'b', 'c']) gives me 'abc'.

 Why this asymmetry? I was under the impression that the two would be
 complementary.


I'm not sure about asymmetry, but how would you implement a split method
with an empty delimiter to begin with? It doesn't make much sense anyway.

If you feel strongly to make it do what you want it to do, it might be a
good idea to submit it to Python-ideas mailing list, or such.

Otherwise, it doesn't hurt to iterate over the string and make use of them,
right? ;)

Cheers,
Xav
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: str.split() with empty separator

2009-09-15 Thread Dave Angel

Ulrich Eckhardt wrote:

Hi!

'abc'.split('') gives me a ValueError: empty separator.
However, ''.join(['a', 'b', 'c']) gives me 'abc'.

Why this asymmetry? I was under the impression that the two would be
complementary.

Uli

  
I think the problem is that join() is lossy;  if you try  .join(['a', 
'bcd', 'e'])   then there's no way to reconstruct the original list with 
split().  Now that can be true even with actual separators,  but perhaps 
this was the reasoning.


Anyway, if you want to turn a string into a list of single-character 
strings, then use

  list(abcde)

DaveA

--
http://mail.python.org/mailman/listinfo/python-list


Re: str.split() with empty separator

2009-09-15 Thread MRAB

Vlastimil Brom wrote:

2009/9/15 Ulrich Eckhardt eckha...@satorlaser.com:

Hi!

'abc'.split('') gives me a ValueError: empty separator.
However, ''.join(['a', 'b', 'c']) gives me 'abc'.

Why this asymmetry? I was under the impression that the two would be
complementary.

Uli



maybe it isn't quite obvious, what the behaviour in this case should be;
re.split also works with empty delimiter (and returns the original string)

re.split(, abcde)

['abcde']

If you need to split the string into the list of single characters
like in your example, list() is the possible way:

list(abcde)

['a', 'b', 'c', 'd', 'e']


I'd prefer it to split into characters. As for re.split, there are times
when it would be nice to be able to split on a zero-width match such as
r\b (word boundary).
--
http://mail.python.org/mailman/listinfo/python-list