Join equivalent for tuples of non-strings?

Discussion in 'Python' started by Peter Hansen, Sep 18, 2003.

  1. Peter Hansen

    Peter Hansen Guest

    "Peter L. Buschman" wrote:
    >
    > I'm trying to think in Python, but am stumped here...
    >
    > What is the equivalent for the following if you are dealing with a tuple
    > of non-strings?
    >
    > >>> import string
    > >>> foo = ( '1', '2', '3' )
    > >>> string.join( foo, '.' )

    > '1.2.3'
    >
    > With a tuple of integers, this fails with a traceback as below:


    Try this instead, using old and new style Python:

    string.join(map(str, foo), '.')

    or

    '.'.join([str(x) for x in foo])

    -Peter
     
    Peter Hansen, Sep 18, 2003
    #1
    1. Advertising

  2. I'm trying to think in Python, but am stumped here...

    What is the equivalent for the following if you are dealing with a tuple
    of non-strings?

    >>> import string
    >>> foo = ( '1', '2', '3' )
    >>> string.join( foo, '.' )

    '1.2.3'

    With a tuple of integers, this fails with a traceback as below:

    >>> bar = ( 1, 2, 3 )
    >>> string.join( bar, '.' )


    Traceback (most recent call last):
    File "<pyshell#11>", line 1, in -toplevel-
    string.join( bar, '.' )
    File "C:\Python23\Lib\string.py", line 135, in join
    return sep.join(words)
    TypeError: sequence item 0: expected string, int found
    >>>


    --PLB
     
    Peter L. Buschman, Sep 18, 2003
    #2
    1. Advertising

  3. Peter Hansen

    Ulrich Petri Guest

    "Peter L. Buschman" <> schrieb im Newsbeitrag
    news:...
    > I'm trying to think in Python, but am stumped here...
    >
    > What is the equivalent for the following if you are dealing with a tuple
    > of non-strings?
    >
    > >>> import string
    > >>> foo = ( '1', '2', '3' )
    > >>> string.join( foo, '.' )

    > '1.2.3'
    >
    > With a tuple of integers, this fails with a traceback as below:
    >
    > >>> bar = ( 1, 2, 3 )
    > >>> string.join( bar, '.' )

    >


    try:

    foo = (1,2,3,4,5,6)
    ".".join([str(x) for x in foo])

    HTH

    Ciao Ulrich
     
    Ulrich Petri, Sep 18, 2003
    #3
  4. Awesome! Thanks guys :)
    --PLB
     
    Peter L. Buschman, Sep 19, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Silver
    Replies:
    0
    Views:
    892
    Alan Silver
    Jun 5, 2006
  2. Replies:
    5
    Views:
    560
    Thomas J. Gritzan
    Oct 6, 2006
  3. tuples within tuples

    , Oct 26, 2007, in forum: Python
    Replies:
    12
    Views:
    583
    Dennis Lee Bieber
    Oct 27, 2007
  4. xera121
    Replies:
    8
    Views:
    729
    lolmc
    Sep 30, 2009
  5. Jon Reyes
    Replies:
    18
    Views:
    240
    Mitya Sirenef
    Feb 19, 2013
Loading...

Share This Page