bug in string.join()? 
Author Message
 bug in string.join()?

Hi!

Quote:
>>> string.join(("abcde"))

'a b c d e'

Is this the expected behavior, or is it a bug? O:-)

//-----------------------------------------------
//      Fernando Rodriguez Romero
//
//      frr at mindless dot com
//------------------------------------------------



Mon, 11 Aug 2003 03:32:11 GMT  
 bug in string.join()?

Quote:

> >>> string.join(("abcde"))
> 'a b c d e'

> Is this the expected behavior, or is it a bug? O:-)

it's the expected behavior.
string.join concatenate all sequence types. So it iterates
over all characters in the string and joins them with the
default separator ' '.
It's  not a bug - it's a feature!  ;-)

  -Thorsten

--
Anybody else on the list got an opinion?
Should I change the language or not?  (Guido van Rossum, 28 Dec 1991)
from http://www.amk.ca/quotations/python-quotes/page-1.html



Mon, 11 Aug 2003 04:17:22 GMT  
 bug in string.join()?

Quote:


>> >>> string.join(("abcde"))
>> 'a b c d e'

>> Is this the expected behavior, or is it a bug? O:-)
> it's the expected behavior.
> string.join concatenate all sequence types. So it iterates
> over all characters in the string and joins them with the
> default separator ' '.
> It's  not a bug - it's a feature!  ;-)

Also of note is that you have an unnecessary set of parens.  If you
meant to pass a tuple of one element into string.join, you needed to
add a trailing comma.  And you'd get a different behavior:

Quote:
>>> string.join(("abcde"))
'a b c d e'
>>> string.join(("abcde",))

'abcde'


Mon, 11 Aug 2003 04:37:09 GMT  
 bug in string.join()?


Quote:
>>>> string.join(("abcde"))
>'a b c d e'

>Is this the expected behavior, or is it a bug? O:-)

What would you expect it to do? It's already 'joined' without spaces to start
with.

Dan



Mon, 11 Aug 2003 11:06:13 GMT  
 bug in string.join()?
Coffee went everywhere when I read that

Quote:


>>>>> string.join(("abcde"))
>>'a b c d e'

>>Is this the expected behavior, or is it a bug? O:-)

>What would you expect it to do? It's already 'joined' without spaces to start
>with.

>Dan

It threw me somewhat at first:

Quote:
>>> import string
>>> list = ["i've","come","for","an","argument"]
>>> print string.join(list)

i've come for an argument
Quote:
>>> print string.join(list," ")

i've come for an argument
Quote:
>>> print string.join(list,"")

i'vecomeforanargument

so the " " is implicit. i've sometimes thought it would be more intuitive to
have the "" behaviour as the implicit behaviour, but it's simple enough either
way i guess.

luc



Mon, 11 Aug 2003 23:39:20 GMT  
 bug in string.join()?

Quote:

> Coffee went everywhere when I read that



> >>>>> string.join(("abcde"))
> >>'a b c d e'
> >>Is this the expected behavior, or is it a bug? O:-)
> >What would you expect it to do? It's already 'joined' without spaces to start
> >with.
> >Dan
> It threw me somewhat at first:
> >>> import string
> >>> list = ["i've","come","for","an","argument"]
> >>> print string.join(list)
> i've come for an argument
> >>> print string.join(list," ")
> i've come for an argument
> >>> print string.join(list,"")
> i'vecomeforanargument
> so the " " is implicit. i've sometimes thought it would be more intuitive to
> have the "" behaviour as the implicit behaviour, but it's simple enough either

When constructing strings from lists the default value of ' ' as a
delimiter is a pain.  Consider that string.join is often used to replace
string interpolation.  Consider this straw man:

queries = []
for ....
        queries.append("insert into table xyz values '%s','%s','%s' ....

One might prefer to say

queries = []
for .....
        queries.append( map( str, ["insert into table xyz values '", d1, "','"
...

queries = map( string.join, queries )

Of course, for this to work, you would have to say:

queries = map( string.join, queries, ("",)*len(queries))

So, should we propose a join0 that has a delimiter of '', or a version
of map that provides default parameters :-)

Now, to play devils advocate:

' ' makes a convenient delimiter for seperating words in text; it also
matches the defalt delimiter in split.  Notice that:

Quote:
>>> from string import *
>>> split( join( ["i've","come","for","an","arguement"]))

["i've", 'come', 'for', 'an', 'arguement']

If join used '' then word lists could not be broken appart and rejoined
so easily.

Quote:
> way i guess.
> luc

Adam DePrince
Starmedia Network, Inc.
Email:
zlib.decompress('x\332KLI\314\325KI-(\312\314KNu(.I,\312MM\311L\324K\316\317\005\000\221\331\012s')


Mon, 11 Aug 2003 03:33:59 GMT  
 bug in string.join()?

[SNIP]

Quote:
> When constructing strings from lists the default value of ' ' as a
> delimiter is a pain.  Consider that string.join is often used to replace
> string interpolation.  Consider this straw man:

> queries = []
> for ....
> queries.append("insert into table xyz values '%s','%s','%s' ....

> One might prefer to say

> queries = []
> for .....
> queries.append( map( str, ["insert into table xyz values '", d1, "','"
> ...

> queries = map( string.join, queries )

> Of course, for this to work, you would have to say:

> queries = map( string.join, queries, ("",)*len(queries))

Death to map! Use list comprehensions instead. And string methods! New is
good, old is bad <0.9 wink>.

   queries = [''.join(q) for q in {*filter*}ies]

[SNAP]

-tim



Tue, 12 Aug 2003 05:13:23 GMT  
 bug in string.join()?


Quote:
> Death to map! Use list comprehensions instead.

As soon as my 1940s brain comprehends them.  ;-)  --john

--



Tue, 12 Aug 2003 13:16:19 GMT  
 bug in string.join()?

Quote:



>>>>> string.join(("abcde"))
>>'a b c d e'

>>Is this the expected behavior, or is it a bug? O:-)

>What would you expect it to do? It's already 'joined' without spaces to start
>with.

That's why I expected it to do nothing. Instead, it did something weird.

//-----------------------------------------------
//      Fernando Rodriguez Romero
//
//      frr at mindless dot com
//------------------------------------------------



Wed, 13 Aug 2003 07:47:20 GMT  
 bug in string.join()?
OK, first of all, according to the docs, string.join operates on tuples and
lists, not strings. If you change your code to string.join(("abcde",)), you
get what you expected.

But string.join will actually except anything sequence-type like and, by
default, insert a space between it's items. Which is exactly what it did.

Quote:
-----Original Message-----


Sent: Friday, February 23, 2001 3:47 PM

Subject: Re: bug in string.join()?





>>>>> string.join(("abcde"))
>>'a b c d e'

>>Is this the expected behavior, or is it a bug? O:-)

>What would you expect it to do? It's already 'joined' without spaces to
start
>with.

That's why I expected it to do nothing. Instead, it did something weird.

//-----------------------------------------------
//      Fernando Rodriguez Romero
//
//      frr at mindless dot com
//------------------------------------------------
--
http://mail.python.org/mailman/listinfo/python-list



Wed, 13 Aug 2003 09:20:11 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Oops: difference in operation of string.join and ''.join

2. string.join question

3. string.join() syntax quirky?

4. string.join is abysmally slow

5. Joining string to a variable (newbie)

6. String.join revisited (URGENT for 1.6)

7. string.join() vs % and + operators

8. join vs format for giant strings?

9. Need Help with joining strings, ESL interpreter.

10. Need to join two strings?Help

11. Joining strings versus augmented assignment

12. BUG: Scientific Number to String/String to Number

 

 
Powered by phpBB® Forum Software