From: MRAB on
Neil Cerutti wrote:
> On 2010-06-17, Ian Kelly <ian.g.kelly(a)gmail.com> wrote:
>> On Thu, Jun 17, 2010 at 11:45 AM, Neil Cerutti
>> <neilc(a)norwich.edu> wrote:
>>> What's the best way to do the inverse operation of the .join
>>> function?
>> Use the str.split method?
>
> split is perfect except for what happens with an empty string.
>
I see what you mean.

This is consistent:

>>> ','.join([''])
''
>>> ''.split(',')
['']

but this isn't:

>>> ','.join([])
''
>>> ''.split(',')
['']

An empty string could be the result of .join(['']) or .join([]).

Should .split grow an addition keyword argument to specify the desired
behaviour? (Although it's simple enough to define your own function.)
From: Robert Kern on
On 6/17/10 2:08 PM, Neil Cerutti wrote:
> On 2010-06-17, Ian Kelly<ian.g.kelly(a)gmail.com> wrote:
>> On Thu, Jun 17, 2010 at 11:45 AM, Neil Cerutti
>> <neilc(a)norwich.edu> wrote:
>>> What's the best way to do the inverse operation of the .join
>>> function?
>>
>> Use the str.split method?
>
> split is perfect except for what happens with an empty string.

Why don't you try it and find out?

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

From: Stephen Hansen on
On 6/17/10 12:44 PM, MRAB wrote:
> Neil Cerutti wrote:
>> On 2010-06-17, Ian Kelly <ian.g.kelly(a)gmail.com> wrote:
>>> On Thu, Jun 17, 2010 at 11:45 AM, Neil Cerutti
>>> <neilc(a)norwich.edu> wrote:
>>>> What's the best way to do the inverse operation of the .join
>>>> function?
>>> Use the str.split method?
>>
>> split is perfect except for what happens with an empty string.
>>
> I see what you mean.
>
> This is consistent:
>
>>>> ','.join([''])
> ''
>>>> ''.split(',')
> ['']
>
> but this isn't:
>
>>>> ','.join([])
> ''
>>>> ''.split(',')
> ['']
>
> An empty string could be the result of .join(['']) or .join([]).
>
> Should .split grow an addition keyword argument to specify the desired
> behaviour? (Although it's simple enough to define your own function.)

Guido finds keyword-arguments-to-change-behavior to be unPythonic, IIRC.
It generally means 'make a new API'. But, the question is-- is it worth
the mental strain of a new API?

This is such an extreme edge case, having to do:

if blah:
result = blah.split(',')
else:
result = []

Is really not asking too much, I think.

--

Stephen Hansen
... Also: Ixokai
... Mail: me+list/python (AT) ixokai (DOT) io
... Blog: http://meh.ixokai.io/

From: Neil Cerutti on
On 2010-06-17, Robert Kern <robert.kern(a)gmail.com> wrote:
> On 6/17/10 2:08 PM, Neil Cerutti wrote:
>> On 2010-06-17, Ian Kelly<ian.g.kelly(a)gmail.com> wrote:
>>> On Thu, Jun 17, 2010 at 11:45 AM, Neil Cerutti
>>> <neilc(a)norwich.edu> wrote:
>>>> What's the best way to do the inverse operation of the .join
>>>> function?
>>>
>>> Use the str.split method?
>>
>> split is perfect except for what happens with an empty string.
>
> Why don't you try it and find out?

I'm currently using the following without problems, while reading
a data file. One of the fields is a comma separated list, and may
be empty.

f = rec['codes']
if f == "":
f = []
else:
f = f.split(",")

I just wondered if something smoother was available.

--
Neil Cerutti
From: Robert Kern on
On 6/17/10 3:03 PM, Neil Cerutti wrote:
> On 2010-06-17, Robert Kern<robert.kern(a)gmail.com> wrote:
>> On 6/17/10 2:08 PM, Neil Cerutti wrote:
>>> On 2010-06-17, Ian Kelly<ian.g.kelly(a)gmail.com> wrote:
>>>> On Thu, Jun 17, 2010 at 11:45 AM, Neil Cerutti
>>>> <neilc(a)norwich.edu> wrote:
>>>>> What's the best way to do the inverse operation of the .join
>>>>> function?
>>>>
>>>> Use the str.split method?
>>>
>>> split is perfect except for what happens with an empty string.
>>
>> Why don't you try it and find out?

I would like to apologize. I read that sentence as a question for some reason.

That said, it always helps for you to show the results that you are getting (and
the code that gives those results) and state what results you were expecting.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco